作者:热带彩色鱼_918 | 来源:互联网 | 2023-01-28 22:17
iventwointegerarrays A and B,returnthemaximumlengthofansubarraythatappearsinbotharrays.Exa
iven 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
public int findLength(int[] A, int[] B) {
int len1 = A.length;
int len2 = B.length;
int[][] flag = new int[len1+1][len2+1];
int max =0;
for(int i = 0; i){
for(int j= 0;j){
flag[i+1][j+1]= A[i]==B[j]?flag[i][j]+1:0;
max = flag[i+1][j+1]>max?flag[i+1][j+1]:max;
}
}
return max;
}
LeetCode-718. Maximum Length of Repeated Subarray