leetcode 718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].
Note:
1 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
LCS的改版
int dp[1010][1010];
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int la = A.size();
int lb = B.size();
for (int i = 0; i < la; ++i) dp[i][0] = 0;
for (int i = 0; i < lb; ++i) dp[0][i] = 0;
int ans = 0;
for (int i = 1; i <= la; ++i) {
for (int j = 1; j <= lb; ++j) {
if (A[i-1] == B[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = 0;
ans = max(ans, dp[i][j]);
}
}
return ans;
}
};
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: