# PigyChan_LeetCode 718. 最长重复子数组

## 718. 最长重复子数组

A: [1,2,3,2,1]
B: [3,2,1,4,7]

（1）1 <= len(A), len(B) <= 1000
（2）0 <= A[i], B[i] < 100

#### 代码1.0：

class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int lenA = A.size(), lenB = B.size();
if (lenA < lenB) {
vector<int> temp;
temp = A;
A = B;
B = temp;
}
lenB = B.size();
vector<int> dp(lenB+1);
dp[0] = 0;
int right = 0, left = 0;
int offset = 0;
while (right < lenB)
{
//尚未形成长度>=2的公共子数组
if (left == right)
{
B[right]
}
//已形成长度>=2的公共子数组
}
}
};


#### 代码2.0（已完成）

class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int lenA = A.size(), lenB = B.size();
vector<vector<int>> dp(lenA+1, vector<int>(lenB+1, 0));
dp[0][0] = 0;

int rst = 0;
for (int i = 1; i <= lenA; ++i)
{
for (int j = 1; j<= lenB; ++j)
{
if (A[i - 1] != B[j - 1])  dp[i][j] = 0;
if (A[i - 1] == B[j - 1])  dp[i][j] = dp[i - 1][j - 1] + 1;
rst = max(rst, dp[i][j]);
}
}

return rst;
}
};


04-03 54
03-21 2072

06-09 898
04-09 1万+
04-24 1357
10-24 733