问题描述

给两个整数数组 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. 最长重复子数组的更多相关文章

  1. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  2. Java实现 LeetCode 718 最长重复子数组(动态规划)

    718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...

  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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode:最长公共前缀【14】

    LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...

  8. LeetCode 5 最长对称串

    LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...

  9. LeetCode 81——搜索旋转排序数组 II

    1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...

随机推荐

  1. Table.CombineColumns合并…Combine…(Power Query 之 M 语言)

    数据源: 任意表,表中列数超过两列 目标: 其中两列合并为一列 操作过程: 选取两列>[转换]>[合并列]>选取或输入分隔符>输入新列名>[确定]   M公式:  = T ...

  2. 嵌入式实验一:LED灯点亮

    实验一:LED灯程序 一. 实验环境 开发机环境 ​ 操作系统:ubuntu 12.04 ​ 交叉编译环境:arm-linux-gcc 4.3.2 ​ 6410板子内核源码:linux-3.0.1 目 ...

  3. py常用标准库

    functools python3中增加了更多工具函数,做业务开发时大多情况下用不到,记录一个比较常用的,给函数或者对象初始化参数 partial函数(偏函数) 把一个函数的某些参数设置默认值,返回一 ...

  4. npm ERR! Error: EPERM: operation not permitted

    转载于:https://blog.csdn.net/qq_36772866/article/details/86934950 win10 在npm install时报错 解决方案 删除node-mou ...

  5. Linux(centos)使用docker安装pdf2htmlEX

    pdf2htmlEX是一款可以将pdf文档转换成html文件的插件,但是Linux系统安装起来很麻烦,所以我们使用docker进行安装 首先要安装docker 因为国外镜像很慢,所以我们这边修改使用国 ...

  6. JAVA查询类别(菜单)下的所有子类别(递归)

    /** * 获取父类别下面的所有子类别 * @return List<StoreGoodsCate> 返回当前类别下的所有子类别集合 */ public List<StoreGood ...

  7. MySQL查找数据中相同的数据,并进行删除

    查找表中多余的重复记录,重复记录是根据某个字段来判断 select * from 表名 where 字段 in (select 字段 from 表名 group by 字段 having count( ...

  8. 第一篇CSDN博客,大家好!

    大家好,我是负雪明烛! 我这昵称的来源是喜欢一句很有意蕴的古诗--苍山负雪,明烛天南. 我喜欢这句诗,很多的账号都用了这个"负雪明烛"的昵称,如果大家在其他地方看到叫这个名字的人, ...

  9. 【LeetCode】1134. Armstrong Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接计算 日期 题目地址:https://leetco ...

  10. E. Congruence Equation

    E. Congruence Equation 思路: 中国剩余定理 \(a^n(modp) = a^{nmod(p-1)}(modp)\),那么枚举在\([0,n-2]\)枚举指数 求\(a^i\)关 ...