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. Java和操作系统交互(Java 代码是怎么执行)(转)

    结合 CPU 理解一行 Java 代码是怎么执行的 根据冯·诺依曼思想,计算机采用二进制作为数制基础,必须包含:运算器.控制器.存储设备,以及输入输出设备,如下图所示. 我们先来分析 CPU 的工作原 ...

  2. mac上配置apidoc环境

    1. 安装node.js 和npm 前往 https://nodejs.org/en/ 下载node.js的最新版本,双击.pkg进行安装 在终端输入 node -v ,如正确输出版本号即安装成功 ( ...

  3. vue 之this.$router.push、replace、go的区别

    一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...

  4. 《Python数据科学手册》

    <Python数据科学手册>[美]Jake VanderPlas著 陶俊杰译 Absorb what is useful, discard what is not, and  add wh ...

  5. kubernetes之pod生命周期,pod重启策略, 镜像拉取策略

    pod声明周期(状态):pending , running, succeeded, failed, unknown 挂起(Pending):Pod 已被 Kubernetes 系统接受,但有一个或者多 ...

  6. web开发:javascript案例

    一.浮动与定位复习 二.小米菜单案例 三.轮播图 四.滚动轮播 一.浮动与定位复习 - 浮动与相对定位 ```js// 1.两者均参与布局// 2.主浮动布局, 相对布局辅助完成布局微调// 3.相对 ...

  7. 最近在研究IO

    import java.io.File; import java.io.IOException; public class Demo11_1 { public static void main(Str ...

  8. redis主从+哨兵 安装配置二

    实验环境: 192.168.2.201 centos7 master sentinel 192.168.2.202 centos7 slave   sentinel 192.168.2.203 cen ...

  9. modbus-crc16——c语言

    为确保消息数据的完整性,除了验证消息CRC之外,建议实现检查串行端口(UART)成帧错误的代码.如果接收消息中的CRC与接收设备计算的CRC不匹配,则应忽略该消息.下面的C语言代码片段显示了如何使用逐 ...

  10. C10K问题和多进程模型

    收录编辑来自马哥教育相关课程 内核空间的相关程序在调度用户空间里的进程的时候,也占用了cpu资源...... nginx可以作为两种类型的反向代理 http 和smtp(mail) C10K问题, 当 ...