最长重复子数组有一下性质

A: [1,2,3,2,1]
B: [3,2,1,4,7]
设横是A竖是B,有规律:若横元和竖元相等,则为1,不等为0
1 2 3 2 1
3 0 0 1 0 1
2 0 1 0 1 0
1 1 0 0 0 1
4 0 0 0 0 0
7 0 0 0 0 0
可见最长子数组坐标是(0,2)(1,4)(2,5)
设计dp二维数组,若横元和竖元相等,dp[i][j]=dp[i-1][j-1]有
1 2 3 2 1
3 0 0 1 0 1
2 0 1 0 2 0
1 1 0 0 0 3
4 0 0 0 0 0
7 0 0 0 0 0
则其中最大值3就是答案
class Solution {
int dp[1001][1001];
public:
int findLength(vector<int>& A, vector<int>& B) {
memset(dp, 0, sizeof(dp));
int M = A.size(), N = B.size(), ans = 0;
for(int i = 1; i <= M; i++) {
for(int j = 1; j <= N; j++) {
if(A[i - 1] == B[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
ans = max(ans, dp[i][j]);
}
}
}
return ans;
}
};

【Leetcode】718. 最长重复子数组的更多相关文章

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

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

  2. Java实现 LeetCode 718 最长重复子数组(动态规划)

    718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...

  3. leetcode 718. 最长重复子数组

    问题描述 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出:3 解释: 长度最长的公共子数 ...

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

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

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

  7. LeetCode:最长公共前缀【14】

    LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...

  8. LeetCode 5 最长对称串

    LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...

  9. LeetCode 81——搜索旋转排序数组 II

    1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...

随机推荐

  1. A Child's History of England.10

    In the next reign, which was the reign of Edward, surnamed The Elder, who was chosen in council to s ...

  2. 一个神奇的JS混淆,JSFuck!

    JSFuck,整体由6个字符[, ], (, ), !, +组成,但却是可以正常运行的JS代码,JSFuck程序可以在任何Web浏览器或引擎中运行解释JavaScript! 看一段代码,源代码为:do ...

  3. Hbase与Phoenix整合

    目录 一.简介 二.安装 三.Phoenix Shell操作 SCHEMA操作 1.创建schema 2.使用schema 3.删除schema 表操作 1.显示所有表 2.创建表 3.表数据的增删改 ...

  4. JavaIO——内存操作流、打印流

    我们之前所做的都是对文件进行IO处理,实则我们也可以对内存进行IO处理.我们将发生在内存中的IO处理称为内存流. 内存操作流也可分为两类:字节内存流和字符内存流. (1)ByteArrayInputS ...

  5. Spring Batch : 在不同steps间传递数据

    参考文档: How can we share data between the different steps of a Job in Spring Batch? Job Scoped Beans i ...

  6. 【Python】【Basic】【数据类型】运算符与深浅拷贝

    运算符   1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...

  7. js实现递归菜单无限层

    /*动态加载菜单*/ function dynamicMenu(data){ if (userID != "admin"){ //1.清空所有菜单 $("#menuLis ...

  8. vue 中使用import导入 script 在线链接

    一般我们在vue中导入另外一个文件或者文件中的方法,我们都是使用import来实现他的,那么问题来了,现在我们要导入的不是另外的一个文件,而是在线链接,这该怎么办?我们也使用了 import * as ...

  9. Spring Batch(8) -- Listeners

    September 29, 2020 by Ayoosh Sharma In this article, we will take a deep dive into different types o ...

  10. 【力扣】454. 四数相加 II

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...