[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 ...
随机推荐
- ArrayList 和 Vector 的区别
这两个类都实现了 List 接口( List 接口继承了 Collection 接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态的数组,我们以后可以按位置索引号取 ...
- eNSP 模拟器添加loopback本地回环口
eNSP只能模拟华为的设备,通常情况下数据通信的传递范围仅限于eNSP中的设备之间. 有时我们在学习更多技术,比如希望将eNSP跟VMware Workstation里的虚拟机互通,或者想让eNSP里 ...
- 关于npm run dev报错npm ERR! missing script: dev
出现这个问题应当重新使用 vue init webpack 来初始化工程. 在执行 npm run dev 就可以执行了.
- springcloud(九)-Feign使用Hystrix
前言 上一篇我们使用注解@HystrixCommond的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的,它没有方法体,上一篇讲解的方式显然不适用于Feign. 那么Fei ...
- apache访问快捷方式
<VirtualHost *:80> DocumentRoot "XXX" ServerName XXX Alias /pdodata/ "XXX" ...
- 搭建django环境
一.安装django(两种方式) 1.pip install django 2.python setup.py install(下载gjango包:https://www.djangoproject. ...
- yii学习记录-gii配置
这篇博文中你将了解到以下内容 YiiDemo配置文件 Yii代码生成器 修改PHP执行环境变量 参考 Yii 是一个基于组件.纯OOP的.用于开发大型 Web 应用的高性能PHP框架.它将Web编程中 ...
- 【数组】Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 【es6】变量解构赋值
1.数组解构赋值 let [a,b,c]=[1,2,3];//数组解构赋值,注意:左右两边格式需一致 let [a,b]=[1,2,3];//不完全解构,取位置靠前的值 let [a=1,b]=[un ...
- 18.async函数
1.含义 async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 Generator 函数,依次读取两个文件. var fs = require('fs'); var ...