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. windows下3D文字

    windows下3D文字 简单概述 需要在每一帧的视频图像上面添加3D文字,文字可以自由移动位置,变换各种字体属性,还能进行一些简单动画.然后把处理好的视频图像传个下一个步骤去处理.做的过程中参考了G ...

  2. python初学者日记02(正则表达式)

    写作时间:2018/12/17 作者:永远的码农(博客园) 一.正则表达式简介: 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或R ...

  3. CentOS 6.8安装Ceph

    机器规划 IP 主机名 角色 10.101.0.1 ceph01 mon admin mds 10.101.0.2 ceph02 ods 10.101.0.3 ceph03 ods 10.101.0. ...

  4. mysql中用HEX和UNHEX函数处理二进制数据的导入导出

    读取数据并拼写sql语句,然后进行导入.具体方法为: (1)导出时采用HEX函数读取数据,把二进制的数据转为16进制的字符串: select HEX(binField) from testTable; ...

  5. shell习题第2题:统计ip访问量

    [题目要求] 有日志1.log,部分内容如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com “/seccode. ...

  6. Java OOP——第六章 框架集合

    1.集合框架包含的主要内容及彼此之间的关系: 图1:   集合框架:是为了表示和操作集合而统一规定的一种统一的标准体系结构.               包含三大块的内容:对外的接口.接口的是实现和对 ...

  7. 1. 了解HTML

    HTML概念 HTML,超文本标记语言.它由一套标签组成用来描述网页,值得我们注意的是HTML并不是编程语言,它只是一种标记,我们通过HTML定义了网页的结构,然后再利用其他技术装饰这个结构,赋予这个 ...

  8. mysql的length与char_length的区别

    length:   是计算字段的长度一个汉字是算三个字符,一个数字或字母算一个字符 char_length:不管汉字还是数字或者是字母都算是一个字符 同时这两个函数,可用于判断数据中是否有中文文字 例 ...

  9. openwrt利用openvpn两网互通

    目录 创建证书文件服务器端配置防火墙配置客户端配置uvs-001(远端PC)uvs-002(网关下属设备)测试连接 创建证书文件 安装证书工具 opkg openvpn-easy-rsa 创建证书 b ...

  10. C语言:一个数组中只有两个数字是出现一次

    //1.一个数组中只有两个数字是出现一次, //其他所有数字都出现了两次. //找出这两个数字,编程实现.a //^=单独两个数的^结果 //单独出现的两个数不同位的标记 //position: ^结 ...