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: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1]. 

Note:

  1. 1 <= len(A), len(B) <= 1000
  2. 0 <= A[i], B[i] < 100

思路:

动态规划,dp[i][j]代表长度为i的a与长度为j的b 出现相同的子数组的长度值。

如果a[i-1]==b[j-1]    dp[i][j] = dp[i-1][j-1]+1;

int findLength(vector<int>& A, vector<int>& B)
{
int m = A.size(), n = B.size();
vector<vector<int>>dp(m + , vector<int>(n + , ));
int ret = ; for (int i = ; i <= m;i++)
{
for (int j = ; j <= n;j++)
{
if (i != &&j != )
{
if (A[i - ] == B[j - ])
{
dp[i][j] = + dp[i - ][j - ];
ret = max(ret, dp[i][j]);
}
}
else dp[i][j] = ;
}
}
return ret;
}

[leetcode-718-Maximum Length of Repeated Subarray]的更多相关文章

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

  2. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

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

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

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

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

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

  7. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

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

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

随机推荐

  1. MySQL 5.7修改root密码的4种方法

            sometimes we will forget our password of root in MySQL DB server.so,there're several methods ...

  2. 在SQL Server中批量修改有规律列的定义

    )=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...

  3. linux 第十天学习

    一.RAID 1.常见RAID (RAID 0.RAID1.RAID5.RAID10) 2.RAID 10 阵列添加 2.1.添加硬盘 2.2.查看系统加载 2.3.mdadm 命令添加RAID阵列 ...

  4. Java 8 – Map排序

    前提 Map是Java中最常用的集合类之一,这里整理了关于HashMap的排序 (关于List的排序,请查看Collections.sort()的doc或源码). 将无序的HashMap借助Strea ...

  5. git 完善使用中

    GIT 版本库控制: 第一步:Git 的账号注册 url :https://github.com/ 这是git的官网如果第一次打开会这样 中间红色圈内是注册 内容, 第一项是用户名 第二项是邮箱 第三 ...

  6. xp sp3安装.Net 4.0提示严重错误,0x80070643,解决办法2017版

    客户电脑上要装金税开票软件,需要.net 4.0.30319.1,电脑环境是xp sp3,已经安装了.net 2, .net 3.5sp1,安装.net 4.0的时候提示错误0x80070643 因为 ...

  7. 单片机-C语言-定义和申明

    以下代码是单片机程序,51单片机,编译器为HT-IDE3000, 简单来说 头文件中只能申明, 变量在头文件中申明时,要加上extern 这个关键字用来告诉编译器,变量在其它的文件中定义,为什么要在头 ...

  8. HyperLedger Fabric 1.4 官方End-2-End运行(8)

    8.1 End-2-End案例简介        Fabric官方提供了实现点对点的Fabric网络示例,该网络有两个组织(organizations),一个组织有两种节点(Peer),通过Kafka ...

  9. fedora/centos下gcc编译出现gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory

    fedora/centos下gcc编译出现gcc: error trying to exec 'cc1plus': execvp: No such file or directory解决办法 翻译自: ...

  10. python--模块之re正则表达式

    简介: 正则表达式本身是一个小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,我们可以通过直接调用来实现正则匹配. 正则表达式基础知识: --普通字符匹配自身 abc ----a ...