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. C# 对象的深复制和浅复制

    2019年第一篇博客,好吧,又大了一岁了,继续加油吧. 正文: C# 中的对象,众所周知是引用类型,那么如何复制对象Object呢,我们来看看下面这段代码: public class User { p ...

  2. CentOS 7.x eth0

    1:Test Environment [root@linux-node1 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server rel ...

  3. HTML5之特效

    2D转换 在二维的平面上做一些变化,使用transform属性 1. 2D转换之移动(translate) 案例: div{ width: 200px; height: 200px; backgrou ...

  4. Flask之app实例的参数配置

    说是app实例的配置, 实际也就是flask程序的配置 Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? ...

  5. mysql索引建立原则

    看了网上一些网上关于创建索引的原则,在这里做一下总结: 1.尽量创建在使用频率较高的字段上,比如主键,外键,where总用到的字段,join是相关联的字段 2.如果表过大,一定要创建索引. 3.索引应 ...

  6. 大数据&人工智能&云计算

    仅从技术上讲大数据.人工智能都包含工程.算法两方面内容: 一.大数据: 工程: 1)云计算,核心是怎么管理大量的计算机.存储.网络. 2)核心是如何管理数据:代表是分布式存储,HDFS 3)核心是如何 ...

  7. Python 爬虫 (二)

    cookiejar模块: 管理储存cookie,将传出的http请求添加cookie cookie存储在内存中,CookieJar示例回收后cookie将自动消失 实例:用cookjar访问人人网主页 ...

  8. PAT (Basic Level) Practice 1008 数组元素循环右移问题

    个人练习 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N ...

  9. Wake-Sleep(W-S)算法【转载】

    原文: https://www.zhihu.com/question/29648549 https://blog.csdn.net/zouxy09/article/details/8775518 en ...

  10. solr环境搭建及java小demo

    一配置solr环境 1.下载solr 2.配置solr(最好单独分离出一个tomcat,一台机器启动多个tomcat参见:http://www.cnblogs.com/lxlwellaccessful ...