#week9
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
分析
b a b
c 0 0 0
a 0 1 0
b 1 0 2
a 0 2 0
dp[i][j] = dp[i-1][j-1] + 1;
题解
1 class Solution { 2 public: 3 int findLength(vector<int>& a, vector<int>& b) { 4 int na = a.size(), nb= b.size(); 5 int dp[na+1][nb+1] = {}; 6 int mx = 0; 7 for (int i = 1; i <= na; ++i) for (int j = 1; j <=nb; ++j) { 8 if (a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1] + 1; 9 mx = max(mx,dp[i][j]); 10 } 11 12 return mx; 13 } 14 };
原文地址:https://www.cnblogs.com/iamxiaoyubei/p/8278256.html
时间: 2024-10-08 04:59:57