leetcode 718. 最长重复子数组
问题描述
给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。
示例:
输入:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
输出:3
解释:
长度最长的公共子数组是 [3, 2, 1] 。
提示:
1 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
代码
注意子数组和子序列的区别
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int la = A.size(),lb = B.size(),ans = 0;
vector<vector<int>> dp(la,vector<int>(lb,0));
for(int i = 0; i < la; ++i)
{
if(A[i] == B[0])dp[i][0] = 1;
}
for(int j = 0; j < lb; ++j)
{
if(A[0] == B[j])dp[0][j] = 1;
}
for(int i = 1; i < la; ++i)
{
for(int j = 1; j < lb; ++j)
{
if(A[i] == B[j])
{
dp[i][j] = dp[i-1][j-1] + 1;
ans = max(dp[i][j],ans);
}
}
}
return ans;
}
};
结果:
执行用时:344 ms, 在所有 C++ 提交中击败了71.25%的用户
内存消耗:109 MB, 在所有 C++ 提交中击败了33.33%的用户
leetcode 718. 最长重复子数组的更多相关文章
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- Java实现 LeetCode 718 最长重复子数组(动态规划)
718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...
- 【Leetcode】718. 最长重复子数组
最长重复子数组有一下性质 A: [1,2,3,2,1] B: [3,2,1,4,7]设横是A竖是B,有规律:若横元和竖元相等,则为1,不等为0 1 2 3 2 13 0 0 1 0 12 0 1 0 ...
- [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 ...
- [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:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...
- LeetCode 5 最长对称串
LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...
- LeetCode 81——搜索旋转排序数组 II
1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...
随机推荐
- 阿里面试题: (a,b,c)组合索引, 查询语句select...from...where a=.. and c=..走索引吗?
面试官:(a,b,c)组合索引,查询语句select...from...where a=.. and c=..走索引吗应聘者: 最佳左前缀法,如果索引了多列,要遵守最左前缀法则,否则索引失效 按最左前 ...
- 拖动条形图设置任务关联(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 仅仅是知悉了四种任务关联,那只是纸上谈兵,要把这四种关联真正用到Project上才行,所以我们就要来设置任务关联了. 这是 ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- LuoguP1619 解一元二次方程的烦恼 题解
Content 模拟一个系统,给出一个数 \(n\),让你判断是否是素数,如果是合数的话就要质因数分解. 需要注意的几点: 数字超过 \(4\times 10^7\),输出溢出提示. 数字小于 \(2 ...
- vc++ 调用winapi调节屏幕亮度(增加win7代码demo)
1.关于 代码是通过测试的,测试环境: win7 + MFC 为什么要发在这里? 区别于上一篇随笔. MD排版更顺眼 demo 会放到 这里 更正了上一篇随笔中的代码错误 2.头文件 #include ...
- 15 - Vue3 UI Framework - 完工部署
项目官网也基本完成了,接下来我们再讲一下如何将项目官网部署到 GitHub Pages 上 返回阅读列表点击 这里 项目配置 常见的模式有三种,即 History 模式 Hash 模式 Memory ...
- Visualizing Data using t-SNE
目录 概 主要内容 Stochastic Neighbor Embedding t-SNE Der Maaten L V, Hinton G E. Visualizing data using t-S ...
- The Expressive Power of Neural Networks: A View from the Width
目录 概 主要内容 定理1 定理2 定理3 定理4 定理1的证明 Lu Z, Pu H, Wang F, et al. The expressive power of neural networks: ...
- matplotlib 高阶之Transformations Tutorial
目录 Data coordinates Axes coordinates Blended transformations 混合坐标系统 plotting in physical units 使用off ...
- ios离线打包报错Showing Recent Messages :-1: HBuilder has conflicting provisioning settings. HBuilder is automatically signed for development, but a conflicting code signing identity iPhone Distribution has
1.解决方案找到项目工程文件右击->显示包内容->双击project.pbxproj->搜索distribution改写成Developer