zoukankan      html  css  js  c++  java
  • 718. Maximum Length of Repeated Subarray

    #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 };
  • 相关阅读:
    day⑥:logging模块
    day⑥:shelve模块
    day⑥:xml模块
    day⑤:冒泡排序
    day⑤:模块
    day⑤:re深入
    day④:递归
    day④:迭代器
    day④:装饰器
    day③:函数式编程
  • 原文地址:https://www.cnblogs.com/iamxiaoyubei/p/8278256.html
Copyright © 2011-2022 走看看