Maximum Length of Repeated Subarray:

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 <= len(A), len(B) <= 1000

0 <= A[i], B[i] < 100

这是一道典型的动态规划题目。

寻找子序列的规律如下:



具体思想可见动态规划解最长公共子序列问题

而这是一道找公共子串的题目,和上面不同的是,当xi != yi时,c[i,j]=0。

class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int temp[1001][1001],max = 0;
for(int i = 0; i < A.size(); i++){
for(int j = 0; j < B.size(); j++){
if(A[i] == B[j]){
temp[i+1][j+1] = temp[i][j] + 1;
}else{
temp[i+1][j+1] = 0;
}
max = max>temp[i+1][j+1]?max:temp[i+1][j+1];
}
}
return max;
}
};

[LeetCode]Maximum Length of Repeated Subarray的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. sprintf与strcat

    sprintf可以将整数转化为字符串,也可以连接两个字符串.但是用sprintf在连接两个字符串时,容易出现错误. 因此连接两个字符串时候用strcat, 将整数转化为字符串时候用sprintf. 转 ...

  2. 利用PHP的curl批量导出文件

    场景 1.需要从网站中导出大量的文件,包括有图片格式,文档格式等附件内容. 2.分文件夹来导出不同的数据 <?php header("Content-Type:text/html;ch ...

  3. linux系统解决boot空间不足

    有时候更新Linux系统是会碰到boot空间不足的错误,原因基本上是安装时boot空间设置问题可以通过删除旧的内核来释放boot空间. ubuntu: 1.查看当前使用内核版本号       unam ...

  4. 空行会影响 Java 编译吗?

    简评:往往越简单的问题越容易被人们忽略. 问题 这个月的 Stack Overflow 有篇热门文章是国外有位开发者提出: 当我仅仅对 Java 类增加了一行空行,为什么编译后得到了两个不同的字节码文 ...

  5. Spring Boot Cookbook 中文笔记

    Spring Boot Cookbook 一.Spring Boot 入门 Spring Boot的自动配置.Command-line Runner RESTful by Spring Boot wi ...

  6. java 实现七大基本排序算法

    一. 选择排序 /** * 选择排序: int arr[] = { 5, 6, 2, 7, 8, 6, 4 }; * * 第0趟 5 2 6 7 6 4 8 第1趟 2 5 6 6 4 7 8 第2趟 ...

  7. 收集vcftools所有用法

    VCFtools用来处理VCF文档. 筛选特定突变 比较文件 总结突变 转化文件格式 验证并合并文件 取突变交集和差集 Get basic file statistics input可以为VCF或BC ...

  8. 后台开发面试题(.net与java)

    最近面试了几家公司,发现大部分公司面试题有相似的地方.现在此记录下我还记得的一些题: JAVA部分: 1.Java Map 按Key排序和按Value排序: 参考链接:Java Map 按Key排序和 ...

  9. pygame学习_part1_pygame写程序前的准备工作

    import pygame from pygame import * pygame.init() #准备pygame,不准备无法使用功能 pygame.display.set_mode((x,y坐标) ...

  10. (六)Audio子系统之AudioRecord.release

      在上一篇文章<(五)Audio子系统之AudioRecord.stop>中已经介绍了AudioRecord如何暂停录制,接下来,继续分析AudioRecord方法中的release的实 ...