[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 ...
随机推荐
- C# Winform 小技巧(Datagridview某一列按状态显示不同图片)
步骤: 一.导入状态图片到项目中: 二.在窗体中声明一个图片数组,并在窗体的OnLoad事件中加入图片资源: /// <summary> /// 存储状态图片序列,避免同一状态对图片重复读 ...
- python学习笔记-控制流(if for while break continue)
if语句 if语句用以检查条件:如果条件为真(True),将运行一块语句(称作 if-block 或 if 块),否则将运行另一块语句(称作 else-block 或 else 块).其中else 从 ...
- PCA简单实现
''' 总结一下PCA的算法步骤: 设有m条n维数据. 1)将原始数据按列组成n行m列矩阵X 2)将X的每一行(代表一个属性字段)进行零均值化,即减去这一行的均值 3)求出协方差矩阵C=1/m*(XX ...
- 「Neerc2016」Expect to Wait
题目描述 ls最近开了一家图书馆,大家听说是ls开的,纷纷过来借书,自然就会出现供不应求的情况, 并且借书的过程类 似一个队列,每次有人来借书就将它加至队尾,每次有人来还书就把书借给队头的若干个人,定 ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- secureCRT scripts as vbs
gdb multithread debug. lsusb.py # $language = "VBScript" # $interface = "1.0" Su ...
- Android动画及滑动事件冲突解决(转载)
原文链接:http://blog.csdn.net/singwhatiwanna/article/details/38168103 Android开发中动画和事件处理是程序员迈向高手的必经之路,也是重 ...
- python的socket.recv函数陷阱
目录 前言 一个粘包实验 执行结果 排错思路 解决和总结 前言 惯例练习历史实验,在编写tcp数据流粘包实验的时候,发现一个奇怪的现象.当远程执行的命令返回结果很短的时候可以正常执行,但返回结果很长时 ...
- shiro学习笔记_0400_自定义realm实现身份认证
自定义Realm实现身份认证 先来看下Realm的类继承关系: Realm接口有三个方法,最重要的是第三个方法: a) String getName():返回此realm的名字 b) boolean ...
- redis 数据类型 Hash
Redis 数据类型Hash:hash数据类型存储的数据和mysql数据库中存储的一条记录很类似. hash的一些操作: 比如数据库是user表,有id,name,age ,sex,可以建立与之对应的 ...