``````class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int maxlen = 0;
for(int i = 0;i < A.size();i++)
{
for(int j = 0;j < B.size();j++)
{
if(A[i] == B[j])
{
int k = 1;
while((i + k < A.size())&&(j + k < B.size())
&&(A[i + k] == B[j + k])){
k += 1;
}
maxlen = max(maxlen,k);
}
}
}

return maxlen;
}
};``````

``````Time Limit Exceeded
Details
Last executed input
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0View All``````

dp[i][j] = dp[i-1][j-1]+1 if A[i]==B[j] else 0
dp[i][j] 表示以A[i]结尾 B[j]结尾的数组最长的公共序列长度，如果A[i]和B[j]不相等，那么公共长度为0.（满足以A[i] B[i结尾，最后A[i]] B[j]不相等），如果A[i]和B[j]相等转移到dp[i-1][j-1],这一点相对来说比较好理解。

``````class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int m = A.size(),n = B.size();
int dp[m + 1][n + 1];
memset(dp,0,sizeof(int)*(m+1)*(n+1));
int maxlen = 0;
for(int i = 0; i < m;i++) {
for(int j = 0;j < n;j++) {
if(A[i] == B[j])
dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j - 1] + 1;
else
dp[i][j] = 0;

maxlen = max(maxlen,dp[i][j]);
}
}
return maxlen;
}
};``````

``````Runtime: 160 ms, faster than 91.29% of C++ online submissions for Maximum Length of Repeated Subarray.
Memory Usage: 16 MB, less than 71.61% of C++ online submissions for Maximum Length of Repeated Subarray.``````