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代码

    众所周知, 图像方面的3A算法有: AF自动对焦(Automatic Focus)自动对焦即调节摄像头焦距自动得到清晰的图像的过程 AE自动曝光(Automatic Exposure)自动曝光的是为了 ...

  2. 微信小程序调用api接口

    请求的第三方微信url大概有3种 1)$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&s ...

  3. 转:AbstractQueuedSynchronizer的介绍和原理分析

    引自:http://ifeve.com/introduce-abstractqueuedsynchronizer/ 简介 提供了一个基于FIFO队列,可以用于构建锁或者其他相关同步装置的基础框架.该同 ...

  4. javascript最常用的对象创建方式

    //第一种 function Demo(){ var obj=new Object(); obj.name="Yubaba"; obj.age=12; obj.firstF=fun ...

  5. 关于css属性calc对于ie的态度

    做的一个项目,布局的时候用到了max-height:calc(100% - 15px);在谷歌.火狐浏览器,进行下拉的时候,它的父元素会出现垂直滚动条,但是在IE就不可以. 然后在网上找了找,说在它的 ...

  6. jquery实现表单验证简单实例

    /* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...

  7. java 整型数据转换为小数类型 BigDecimal 装换为Double

    A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...

  8. windows下nginx的安装

    一. 下载 http://nginx.org/    (下载后解压) 二. 修改配置文件 nginx配置文件在 nginx-1.8.0\conf\nginx.conf http { gzip on; ...

  9. python教程(一)·简介

    先简单介绍下python.(真的很简单) python是什么? Python是Guido van Rossum发布于1991年的一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,是一种解释型的 ...

  10. notpad++ 搭配 gcc

    notpad++ 搭配 gcc GCC 是 GNU 编译器套装的简称(GNU Compiler Collection),一套编程语言编译器,以 GPL 及 LGPL 许可证所发行的自由软件,也是 GN ...