码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-718. Maximum Length of Repeated Subarray

时间:2019-07-09 22:21:59      阅读:125      评论:0      收藏:0      [点我收藏+]

标签: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:

  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;
    }

 

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!