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

718. Maximum Length of Repeated Subarray

时间:2018-01-13 11:09:31      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:turn   note   cto   size   and   xpl   mil   nbsp   exp   

#week9

Given 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

 

分析

   b  a  b

c  0  0  0

a  0  1  0

b  1  0  2

a  0  2  0

dp[i][j] = dp[i-1][j-1] + 1;

题解

 1 class Solution {
 2 public:
 3     int findLength(vector<int>& a, vector<int>& b) {
 4         int na = a.size(), nb= b.size();
 5         int dp[na+1][nb+1] = {};
 6         int mx = 0;
 7         for (int i = 1; i <= na; ++i) for (int j = 1; j <=nb; ++j) {
 8             if (a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
 9             mx = max(mx,dp[i][j]);
10         }
11         
12         return mx;
13     }
14 };

 

718. Maximum Length of Repeated Subarray

标签:turn   note   cto   size   and   xpl   mil   nbsp   exp   

原文地址:https://www.cnblogs.com/iamxiaoyubei/p/8278256.html

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