leetcode - 718. Maximum Length of Repeated Subarray

动态规划
在这里插入图片描述

class Solution {
public:
    int findLength(vector<int>& A, vector<int>& B) {
        int m=A.size(),n=B.size();
        int count=0;
        for(int i=0;i<=m;++i)
        {
            dp[i][0]=0;
        }
        for(int j=0;j<n;++j)
        {
            dp[0][j]=0;
        }
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(A[i-1]==B[j-1])
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    count=max(count,dp[i][j]);
                }else{
                    dp[i][j]=0;
                }
            }
        }
        return count;
    }
private:
    int dp[1001][1001];
    
};

猜你喜欢

转载自blog.csdn.net/weixin_41938758/article/details/88813000