代码随想录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;
    }
};
相关推荐
琢磨先生David1 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245032 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA2 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg12 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA2 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++
岛雨QA2 天前
多路查找树「Java数据结构与算法学习笔记11」
数据结构·算法
AKA__Zas2 天前
初识基本排序
java·数据结构·学习方法·排序