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

Runtime: 78 ms, faster than 40.98% of Java online submissions for Maximum Length of Repeated Subarray.

class Solution {
public int findLength(int[] A, int[] B) {
int[][] dp = new int[A.length+1][B.length+1];
int ret = 0;
for(int i=1; i<dp.length; i++){
for(int j=1; j<dp[0].length; j++){
if(A[i-1] == B[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
ret = Math.max(ret, dp[i][j]);
}
}
}
return ret;
}
}

a better solution

Runtime: 26 ms, faster than 99.59% of Java online submissions for Maximum Length of Repeated Subarray.

有两个关键点,

第一个是内层循环从后往前遍历,如果从前往后遍历就是错误的,因为我们每一次更新dp的时候是dp[j+1] = dp[j] + 1,所以在更新j+1的时候要用到j的信息,而这个j应该是之前的j,也就是上一行的j,可以参考上面一个解法中矩阵的上一行。

第二个是如果没有匹配到,应该把j+1变成0,否则会产生错误的计数。

class Solution {
public int findLength(int[] A, int[] B) {
int[] dp = new int[A.length+1];
int max = 0;
for(int i=0; i<A.length; i++) {
for(int j=B.length-1; j>=0; j--) {
if (A[i] == B[j]) {
dp[j+1] = dp[j] + 1;
if (max < dp[j+1]) {
max = dp[j+1];
}
} else {
dp[j+1] = 0;
}
}
}
return max;
}
}

LC 718. Maximum Length of Repeated Subarray的更多相关文章

  1. Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray

    714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...

  2. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  3. 718. Maximum Length of Repeated Subarray

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  4. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  5. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  6. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  7. [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  8. [Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  9. [leetcode-718-Maximum Length of Repeated Subarray]

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

随机推荐

  1. 7.SpringMVC 配置式开发-ModelAndView和视图解析器

    ModelAndView 1.Model(模型) 1.model的本质就是HashMap,向模型中添加数据,就是往HashMap中去添加数据 2.HashMap 是一个单向查找数组,单向链表数组 3. ...

  2. 【python+selenium】selenium grid(分布式)

    前言 原文:https://blog.csdn.net/real_tino/article/details/53467406 Selenium grid是用来分布式执行测试用例脚本的工具,比如测试人员 ...

  3. cnblogs排版样式预览

    说明:关于本博主题及样式来源于[GitHub]:本博总体排版目录样式风格参照博文[修仙成神之路]进行预览:参照本博设置可参考博文[设置跟本博一样的效果]本博之前发表过的博文存在样式不协调,后期会逐一完 ...

  4. php-fpm优化内存占用大

    1.1 Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题 LNMP架构中PHP是运行在FastCGI模式下,按照官方的说法,php-cgi会在每个请求结束的时候会回收脚 ...

  5. VS---《在VS2010中 使用C++创建和使用DLL》(002)

    VS---<在VS2010中 使用C++创建和使用DLL>(002) 前面初认识了创建和调用DLL,在VS工程下可以通过在 同一工程.不同工程 下创建和调用DLL.现在,同一工程下创建和调 ...

  6. 用union 和 struct 位域操作

    很久没有用C 语言中的 union 和 struct 位域操作了. 最近用了一下(当然,我承认是从stackoverflow 上抄的) 需求是这样的,已知一个 LPARAM 整数 3866625 ,求 ...

  7. 误删rpm命令的恢复方法

    rpm命令不能用了,被依赖的yum也不能使用了, 恢复rpm命令无外乎重装, 重装方法1: 使用源码编译,  需要gcc ,cmake包,如果没装,悲剧了 重装方法2: 找一台,和出问题的这台同样系统 ...

  8. 深入了解java线程池(转载)

    出处:http://www.cnblogs.com/dolphin0520/ 本文归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责 ...

  9. 题解 [SDOI2010] 大陆争霸

    题面 解析 这题似乎不是那么难啊 首先,显而易见, 如果要摧毁一个城市,必须要满足两个条件: 机器人摧毁了保护它的城市. 机器人到达了这个城市. 而这两个条件可以同时进行(毕竟有无数机器人) 那么显然 ...

  10. JavaScript程序设计——FOR循环

    FOR循环流程图: 1.编写求6!的阶乘的代码 2.编写10个10相加的和 3.编写1+2+3+...+10连续相加的和 4.编写1+(1+2)+(1+2+3)+...+(1+2+3+...+10)连 ...