本文主要是介绍代码随想录训练营day43|动态规划part10,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最长递增子序列
力扣题目链接
class Solution {
public:int lengthOfLIS(vector<int>& nums) {int len = nums.size();int result = 1;vector<int> dp(len, 1);for(int i = 1; i < len; i++){for(int j = 0; j < i; j++){if(nums[j] < nums[i]){dp[i] = max(dp[i], dp[j]+1);}result = max(result, dp[i]);}}return result;}
};
最长连续递增序列
力扣题目链接
class Solution {
public:int findLengthOfLCIS(vector<int>& nums) {int len = nums.size();int result = 1;vector<int> dp(len, 1);for(int i = 1; i < len; i++){if(nums[i] > nums[i-1]){dp[i] = dp[i-1]+1;result = max(result, dp[i]);}}return result;}
};
最长重复子数组
力扣题目链接
class Solution {
public:int findLength(vector<int>& nums1, vector<int>& nums2) {int len1 = nums1.size();int len2 = nums2.size();int result = 0;// 以i,j为结尾的重复子序列的长度vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));for(int i = 1; i <= len1; i++){for(int j = 1; j <= len2; j++){if(nums1[i-1] == nums2[j-1]){dp[i][j] = dp[i-1][j-1] + 1;result = max(result, dp[i][j]);}}}return result;}
};
这篇关于代码随想录训练营day43|动态规划part10的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!