代码随想录Day52——300.最长递增子序列 674. 最长连续递增序列 718. 最长重复子数组

300.最长递增子序列

给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。

子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        vector<int> dp(nums.size(),1);
        int result = 1;
        for(int i = 1;i<nums.size();i++)
        {
            for(int j = 0;j<i;j++)
            {
                if(nums[i]>nums[j]) dp[i] = max(dp[i],dp[j]+1);
            }
            result = max(result,dp[i]);
        }
        return result;
    }
};

674. 最长连续递增序列

给定一个未经排序的整数数组,找到最长且连续递增的子序列,并返回该序列的长度。

连续递增的子序列 可以由两个下标 lrl < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。

cpp 复制代码
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        vector<int> dp(nums.size(),1);
        int result = 1;
        for(int i = 1;i<nums.size();i++)
        {
            if(nums[i] > nums[i-1]) dp[i] = dp[i-1]+1;
            result = max(result,dp[i]);
        }
        return result;
    }
};

718. 最长重复子数组

给两个整数数组 nums1nums2 ,返回 两个数组中 公共的 、长度最长的子数组的长度

cpp 复制代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp(nums1.size()+1,vector<int>(nums2.size()+1,0));
        int result = 0;
        for(int i = 0;i<nums1.size();i++)
        {
            for(int j = 0;j<nums2.size();j++)
            {
                if(nums1[i] == nums2[j]) dp[i+1][j+1] = dp[i][j] + 1;
                result = max(result,dp[i+1][j+1]);
            }
        }
        return result;
    }
};
相关推荐
吴声子夜歌39 分钟前
OpenCV——Mat类及常用数据结构
数据结构·opencv·webpack
笑口常开xpr1 小时前
数 据 结 构 进 阶:哨 兵 位 的 头 结 点 如 何 简 化 链 表 操 作
数据结构·链表·哨兵位的头节点
@我漫长的孤独流浪2 小时前
数据结构测试模拟题(4)
数据结构·c++·算法
YGGP6 小时前
吃透 Golang 基础:数据结构之 Map
开发语言·数据结构·golang
weixin_419658317 小时前
数据结构之栈
数据结构
图先7 小时前
数据结构第一章
数据结构
草莓熊Lotso8 小时前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
Andrew_Xzw10 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
超的小宝贝11 小时前
数据结构算法(C语言)
c语言·数据结构·算法
凤年徐13 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表