[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 ...
随机推荐
- django入门-视图-part3
尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6511384.html 完全翻译自官方文档 https://docs.djangoproje ...
- 基于Dubbo的Hessian协议实现远程调用
Dubbo基于Hessian实现了自己Hessian协议,可以直接通过配置的Dubbo内置的其他协议,在服务消费方进行远程调用,也就是说,服务调用方需要使用Java语言来基于Dubbo调用提供方服务, ...
- jquery源码解析:attr,prop,attrHooks,propHooks详解
我们先来看一下jQuery中有多少个方法是用来操作元素属性的. 首先,看一下实例方法: 然后,看下静态方法(工具方法): 静态方法是内部使用的,我们外面使用的很少,实例方法才是对外的. 接下来,我们来 ...
- 某种带权有向无环图(graph)的所有路径的求法
// 讨论QQ群:135202158 最近做某个东西,最后用图实现了,这里总结一下算法. 假设有以下带权有向无环图(连通或非连通,我这里用的是非连通的): 每个节点(node)可能与其他节点有向地相连 ...
- [CISCO] 思科交换机基本配置
思科交换机基本配置 交换机是局域网中最重要的设备,交换机是基于 MAC 来进行工作的.和路由器类似,交换 机也有 IOS,IOS 的基本使用方法是一样的.本章将简单介绍交换机的一些基本配置,以及交换 ...
- 不信任的 .exe 怎么办,用 Windows 沙盒啊!
简评:维基百科,在计算机安全领域,沙盒(sandbox)是种安全机制,为执行中的程式提供的隔离环境.通常是作为一些来源不可信.具破坏力或无法判定程序意图的程序提供实验之用. 微软正在尝试解决人们对运行 ...
- canvas+js+面向对象的圆形封装
效果: Circle.js /* 1. 封装属性: x, y r, fillStyle strokeStyle opacity 2.render */ function Circle(option) ...
- sparksql连接mysql
1.方法1:分别将两张表中的数据加载为DataFrame /* * 方法1:分别将两张表中的数据加载为DataFrame * */ /* Map<String,String> option ...
- 用Jquery获取Url的参数
在网上找的一个办法 //先写一个方法 function GetUrlString(name) { var reg = new RegExp("(^|&)"+ name +& ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...