[LeetCode]Maximum Length of Repeated Subarray
Maximum Length of Repeated Subarray:
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: 3Explanation:
The repeated subarray with maximum length is [3, 2, 1].Note:
1 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
这是一道典型的动态规划题目。
寻找子序列的规律如下:
具体思想可见动态规划解最长公共子序列问题。
而这是一道找公共子串的题目,和上面不同的是,当xi != yi时,c[i,j]=0。
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int temp[1001][1001],max = 0;
for(int i = 0; i < A.size(); i++){
for(int j = 0; j < B.size(); j++){
if(A[i] == B[j]){
temp[i+1][j+1] = temp[i][j] + 1;
}else{
temp[i+1][j+1] = 0;
}
max = max>temp[i+1][j+1]?max:temp[i+1][j+1];
}
}
return max;
}
};
[LeetCode]Maximum Length of Repeated Subarray的更多相关文章
- [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 ...
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 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 ...
- 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 ...
- [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 ...
- [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-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 - AC算法
上篇文章(http://www.cnblogs.com/zzqcn/p/3508442.html)里提到的BF和KMP算法都是单模式串匹配算法,也就是说,模式串只有一个.当需要在字符串中搜索多个关键字 ...
- scrapy 中用selector来提取数据的用法
一. 基本概念 1. Selector是一个可独立使用的模块,我们可以用Selector类来构建一个选择器对象,然后调用它的相关方法如xpaht(), css()等来提取数据,如下 from sc ...
- 总结day3 ---- 进制转换,字符串切片,字符串常用方法.,for 循环,
前情提要: int 的相关操作 进制转换 bit_lenth() str 的索引,以及常用的相关方法 for 循环 索引 切片 相关方法 一 : int 的相关操作 int 主要用于生活中的计算问题 ...
- secureCRT scripts as vbs
gdb multithread debug. lsusb.py # $language = "VBScript" # $interface = "1.0" Su ...
- .NET(C#):使用反射来获取枚举的名称、值和特性
首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...
- redis 数据库迁移
老大让把 一台机器上 redis 中所有的数据,迁移到另一台机器上 查了一下可以拷贝 rdb 文件, 此方法只适用于迁移到一个新的库, 迁移到正在使用的库就不行了, 而且 rdb 里面是所有的 db, ...
- 深度学习(二)BP求解过程和梯度下降
一.原理 重点:明白偏导数含义,是该函数在该点的切线,就是变化率,一定要理解变化率. 1)什么是梯度 梯度本意是一个向量(矢量),当某一函数在某点处沿着该方向的方向导数取得该点处的最大值,即函数在该点 ...
- PHP之string之ord()函数使用
ord (PHP 4, PHP 5, PHP 7) ord - Return ASCII value of character ord - 返回字符的 ASCII 码值 Description int ...
- Redis: under the hood---转载
http://pauladamsmith.com/articles/redis-under-the-hood.html#redis-under-the-hood How does the Redis ...
- 关于安装多个版本jdk之后java -version不正确的问题
问题描述: 今天突然想写一个socket通信的小应用,分别采用BIO.NIO.AIO的方式来实现,来复习前面看的关于TCP/UDP通信的知识.于是乎在原来安装了jdk1.6的机子上重新安装了jdk1. ...