LeetCode-718. Maximum Length of Repeated Subarray

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. 1 <= len(A), len(B) <= 1000
  2. 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<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;
    }

 

上一篇:ConcurrentHashMap之tableSizeFor()方法透析(位运算运用)


下一篇:Project Euler 67: Maximum path sum II