【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
【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 <= len(A), len(B) <= 1000
- 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)的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- [LeetCode]Maximum Length of Repeated Subarray
Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...
- [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 ...
- [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 ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
随机推荐
- 42-Remove Nth Node From End of List
Remove Nth Node From End of List My Submissions QuestionEditorial Solution Total Accepted: 106592 To ...
- 单片机ISP、IAP和ICP几种烧录方式的区别
单片机ISP.IAP和ICP几种烧录方式的区别 玩单片机的都应该听说过这几个词.一直搞不太清楚他们之间的区别.今天查了资料后总结整理如下. ISP:In System Programing,在系统编程 ...
- 分布式事务(4)---最终一致性方案之TCC
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(3)---强一致性分布式事务Atomikos实战 强一致性分布式事务解决方案要求参与事务的各个节点的数据时刻保 ...
- 一个专业处理字符串的IDEA插件
字符串处理想必是小伙伴们平时开发时经常碰到的一个 "难题".为什么要打上引号?因为你说他难吧,其实也不是什么特别复杂的事:你说他不难吧,弄起来还真挺麻烦的,像删除其中空行啊.切换大 ...
- 学习java 7.25
学习内容: 特殊边框 1. TitledBorder:它的作用并不是直接为其他组件添加边框,而是为其他边框设置标题,创建该类的对象时,需要传入一个其他的Border对象; 2. CompoundBor ...
- 容器的分类与各种测试(三)——deque
deque是双端队列,其表象看起来是可以双端扩充,但实际上是通过内存映射管理来营造可以双端扩充的假象,如图所示 比如,用户将最左端的buff用光时,map会自动向左扩充,继续申请并映射一个新的buff ...
- 【分布式】Zookeeper伪集群安装部署
zookeeper:伪集群安装部署 只有一台linux主机,但却想要模拟搭建一套zookeeper集群的环境.可以使用伪集群模式来搭建.伪集群模式本质上就是在一个linux操作系统里面启动多个zook ...
- 哪里可以下载支付宝demo或者sdk
http://club.alipay.com/read-htm-tid-9976972.html 这里有所有的demo和sdk包括移动产品的demo.在他的论坛里面呢 真心恶心啊.不放到主页.
- GO 时间处理
比较大小 比较大小 先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可. time1 := "2015-03-20 08:50:29& ...
- How does “void *” differ in C and C++?
C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; th ...