LeetCode-718. Maximum Length of Repeated Subarray
Posted zhacai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 <= 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<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的主要内容,如果未能解决你的问题,请参考以下文章
[leetcode] 718. Maximum Length of Repeated Subarray
LeetCode-718. Maximum Length of Repeated Subarray
Leetcode 718. Maximum Length of Repeated Subarray