代码随想录训练营第五十二天|300.最长递增子序列,674. 最长连续递增序列,718. 最长重复子数组

300.最长递增子序列

题目链接:https://leetcode.cn/problems/longest-increasing-subsequence/submissions/

代码:

class Solution {
public:
    int lengthOfLIS(vector& nums) {
        vector> dp(nums.size(),vector(2,0));   //0:该序列最后一个 1:数量
        dp[0][0] = nums[0];
        dp[0][1] = 1;
        int result = 1;
        for(int i = 1; i < nums.size(); i++)
        {
            int j = i-1;
            dp[i][0] = nums[i];
            while(j >= 0)
            {
                if(nums[i] > dp[j][0])
                {
                    dp[i][1] = max(dp[j][1]+1,dp[i][1]);
                }
                j--;
            }
            if(dp[i][1] == 0)
                dp[i][1] = 1;
            if(dp[i][1] > result)
                result = dp[i][1];
        }
        return result;
    }
};

674. 最长连续递增序列

题目链接:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/submissions/

代码:

class Solution {
public:
    int findLengthOfLCIS(vector& nums) {
        vector dp(nums.size(),0);
        dp[0] = 1;
        int result = 1;
        for(int i = 1; i < nums.size(); i++)
        {
            if(nums[i] > nums[i-1])
            {
                dp[i] = dp[i-1]+1;
            }else
            {
                dp[i] = 1;
            }
            if(dp[i] > result)
                result = dp[i];
        }
        return result;
    }
};

718. 最长重复子数组

题目链接:https://leetcode.cn/problems/maximum-length-of-repeated-subarray/submissions/

代码(暴力 超时了):

class Solution {
public:
    int Length(vector& nums1, vector& nums2,int pos1,int pos2)
    {
        int result = 0;
        int i = pos1;
        int j = pos2;
        while(i < nums1.size() && j < nums2.size())
        {
            if(nums1[i] == nums2[j])
                result++;
            else    return result;
            i++;
            j++;
        }
        return result;
    }
    int findLength(vector& nums1, vector& nums2) {
        // vector> dp(2,vector(nums1.size(),1));  //0:nums1 1:nums2 
        int result = 0;
        int tmp = 0;
        for(int i = 0; i < nums1.size(); i++)
        {
            int j = 0;
            for(j;j < nums2.size(); j++)
            {
                if(nums2[j] == nums1[i])
                {
                    tmp = Length(nums1,nums2,i,j);
                    if(tmp > result)
                        result = tmp;
                }
            }
        }
        return result;
    }
};

代码(动态规划):

class Solution {
public:
    int findLength(vector& nums1, vector& nums2) {
        vector> dp(nums1.size()+1,vector(nums2.size()+1,0));
        //dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最长重复子数组长度为dp[i][j]
        int result = 0;
        for(int i = 1; i <= nums1.size(); i++)
        {
            for(int j = 1;j <= nums2.size(); j++)
            {
                if(nums1[i-1] == nums2[j-1])
                    dp[i][j] = dp[i-1][j-1]+1;
                if(dp[i][j] > result)
                    result = dp[i][j];
            }
        }
        return result;
    }
};

dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最长重复子数组长度为dp[i][j]。

特别注意: “以下标i - 1为结尾的A” 标明一定是 以A[i-1]为结尾的字符串 )

你可能感兴趣的:(c++)