标签:bsp class sub subarray repeat arrays xpl i+1 array
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:
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<len1;i++){ for(int j= 0;j<len2;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
标签:bsp class sub subarray repeat arrays xpl i+1 array
原文地址:https://www.cnblogs.com/zhacai/p/11160652.html