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. python多线程知识-实用实例

    python多线程使用场景:IO操作,不适合CPU密集操作型任务   1.多个线程内存共享 2.线程同时修改同一份数据需要加锁,mutex互斥锁 3.递归锁:多把锁,锁中有锁 4.python多线程, ...

  2. 2018 CVTE 前端校招笔试题整理

    昨天晚上(7.20)做了CVTE的前端笔试,总共三十道题,28道多选题,2道编程题 .做完了之后觉得自己基础还是不够扎实,故在此整理出答案,让自己能从中得到收获,同时给日后的同学一些参考. 首先说一下 ...

  3. .Net core 使用Jenkins + Docker + Azure Devops 傻瓜式部署

    这几天一直在搞 Jenkins + Docker + Azure Devops 部署,因为这种方式部署真的非常的省心,而且速度快,方便快捷,等等无数优点,感谢我的单身领导的支持,当然也感谢 晓晨大神, ...

  4. MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解

    mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...

  5. linux系统环境下的静态库和动态库的制作

    linux系统下的应用编程需要系统提供的库文件,包括静态库或动态库.不管是静态库还是动态库,都是编译好的二进制文件.在我们编译程序时要链接的目标文件,静态库是链接的时候直接编译到程序里,和程序成为一体 ...

  6. Docker之centos 简单安装

    centos6因为系统自带的可执行的应用程序与 docker 包名字发生冲突,所以重新命名 docker 的RPM包名字为 docker-io. 首先卸载docker包 sudo yum -y rem ...

  7. Apache Flume简介及安装部署

    概述 Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的软件. Flume 的核心是把数据从数据源(source)收集过来,再将收集到的数据送到指定的目 ...

  8. Linux3.5—IIC学习分析

    I2C控制器的设备对象内核已经实现并关联到platform总线. I2C控制器的驱动对象内核已经实现. 看mach-tiny4412.h /plat-samsung/目录下 /drivers/i2c/ ...

  9. json传值给前端页面,出现堆栈溢出问题

    用的com.alibaba.fastjson.JSONObject这个包 原因:JSONObject将对象转json字符串时候没有关闭循环引用导致的堆栈溢出. 解决办法是 使用这个 JSONObjec ...

  10. Java两个线程实现交替运行-以交替打印奇偶数为例

    本文旨在两个线程交替运行,不多哔哔直接看代码吧 public class Work2 { static final Object object = new Object(); public stati ...