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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/

题目描述:

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

题目大意

求最长重复子数组。那么如果我们将数组换成字符串,实际这道题就是求Longest Common Substring的问题了。

解题方法

这个题显然是DP。一定注意,必须连续才行!那么dp数组中每个不为0的位置,一定是两者相等的地方。

比如,对于这两个数组[1,2,2]和[3,1,2],我们的dp数组为:

  3 1 2
1 0 1 0
2 0 0 2
2 0 0 1

所以递推关系为,dp[i][j] = dp[i-1][j-1],当A[i]== B[j]。如果不等的话,dp[i][j]为0.

刚开始理解成了最长子序列Longest Common Subsequence问题了。耽误了不少时间……

代码如下:

class Solution:
def findLength(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
m, n = len(A), len(B)
dp = [[0 for j in range(n + 1)] for i in range(m + 1)]
max_len = 0
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif A[i - 1] == B[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
max_len = max(max_len, dp[i][j])
return max_len

换一种方式写,可能更好理解吧,毕竟少了一行和一列空的0.

class Solution:
def findLength(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
m, n = len(A), len(B)
dp = [[0 for j in range(n)] for i in range(m)]
max_len = 0
for i in range(m):
for j in range(n):
if A[i] == B[j]:
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j - 1] + 1
max_len = max(max_len, dp[i][j])
return max_len

参考资料:
http://www.cnblogs.com/grandyang/p/7801533.html

日期

2018 年 9 月 11 日 ———— 天好阴啊

【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)的更多相关文章

  1. [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 ...

  2. 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 ...

  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. LC 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】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

随机推荐

  1. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  2. day34 前端基础之JavaScript

    day34 前端基础之JavaScript ECMAScript 6 尽管 ECMAScript 是一个重要的标准,但它并不是 JavaScript 唯一的部分,当然,也不是唯一被标准化的部分.实际上 ...

  3. Mockito 简介

    Mockito 是一种 Java Mock 框架,主要是用来做 Mock 测试,它可以模拟任何 Spring 管理的 Bean.模拟方法的返回值.模拟抛出异常等等,在了解 Mockito 的具体用法之 ...

  4. 【Word】自动化参考文献-交叉引用

    第一步:设置参考文献标号 开始-定义新编号格式中,定义参考文献式的方框编号: 这里注意不要把他原来的数字去掉 第二步:选择交叉引用 插入-交叉引用: 第三步:更新标号 如果更新标号,使用右键-更新域. ...

  5. Quartz在.NET中的使用

    一.背景 例如需要在某年某月去将数据库的某个数据更新或者同步,又或者是每隔一段时间来执行一部分代码去调用接口,但是又不想人为的手动去执行 针对此类业务可以使用"定时调用任务",市面 ...

  6. git push大文件失败(write error: Broken pipe)完美解决

    问题 在使用git push推送大文件(超过了100MB)到GitHub远程仓库时提示异常,异常信息如下: fatal: sha1 file '<stdout>' write error: ...

  7. 持续部署CI/CD

    一.简介 在敏捷开发时,通常将服务进行拆分成不同模块,每个开发小组负责一个模块的开发,会在一天内对这个模块进行频繁的提交到仓库主干并部署到线上.CI/CD就是在开发中使用工具保证快速并稳定上线的方法, ...

  8. Jenkins环境变量

    目录 一.环境变量 二.自定义环境变量 三.自定义全局变量 四.常用变量定义 五.常用环境变量 一.环境变量 环境变量可以被看作是pipeline与Jenkins交互的媒介.比如,可以在pipelin ...

  9. house of force----gyctf_2020_force!!

    做这道题前线学习一下house of force的用法 Linux下堆溢出利用2-House of force基本原理_haibiandaxia的博客-CSDN博客 老样子例行检查(这里我就不放了) ...

  10. 周期性任务(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 话说录入任务信息是件体力活,每个任务都是要一个字一个字码出来滴.要说一个项目文件,任务内容是主体,所以这作为体力活也不冤枉 ...