【Leetcode】718. 最长重复子数组
最长重复子数组有一下性质
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. 最长重复子数组的更多相关文章
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- Java实现 LeetCode 718 最长重复子数组(动态规划)
718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...
- leetcode 718. 最长重复子数组
问题描述 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出:3 解释: 长度最长的公共子数 ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...
- LeetCode 5 最长对称串
LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...
- LeetCode 81——搜索旋转排序数组 II
1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...
随机推荐
- pow()是如何实现的?
如1.5 ** 2.5,如何计算?似乎是这样的: 1. cmath calculates pow(a,b) by performing exp(b * log(a)). stackoverflow 2 ...
- acute, adapt
acute In Euclidean [欧几里得] geometry, an angle is the figure [图形] formed by two rays, called the sides ...
- [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现
漏洞影响范围 Yii2 < 2.0.38 环境搭建 Yii2.0.37 漏洞分析 首先定位到漏洞起始点 为什么是这儿?我们该怎么发现是某个类的某个函数?为什么不是其他函数? 一般是__destr ...
- 【Linux】【Basis】【Kernel】Linux常见系统调用
一,进程控制 1)getpid,getppid--获取进程识别号 #include <sys/types.h> #include <unistd.h> pid_t getpid ...
- Linux:$?,$n,$#,$0
$? 获取执行上一个指令的返回值(0为成功,非零为失败) $n 获取当前执行的shell脚本的第n个参数值,n=1...9,当n=0的时表示脚本的文件名,如果n大于9,大括号括起来${10} $# 获 ...
- Spring Boot事务支持
一.创建项目 二.添加依赖 <dependencies> <dependency> <groupId>org.projectlombok</groupId&g ...
- Windows下搭建FFmpeg开发调试环境
背景 如果你是一个FFmpeg的使用者,那么绝大部分情况下只需要在你的程序中引用FFmpeg的libav*相关的头文件,然后在编译阶段链接相关的库即可. 但是如果你想调试FFmpeg内部相关的逻辑,或 ...
- Linux 目录结构及详细操作
目录 Linux 目录结构及详细操作 目录结构 目录结构的特点 目录结构挂载 目录结构发展 关闭selinux(了解) 重要目录说明(etc目录说明) 1.网卡配置文件 2.解析配置文件 3.主机名称 ...
- JUC之Lock接口以及Synchronized回顾
Lock接口 Synchronized关键字回顾: 多线程编程步骤(上): 创建资源类,在资源类创建属性和操作方法 创建多个线程,调用资源类的操作方法 创建线程的四种方式: 继承Thread 实现Ru ...
- heap exploit about ptmalloc in glibc version 2.31
学习的一下高版本的libc的利用方式. 项目地址:https://github.com/StarCross-Tech/heap_exploit_2.31 tcache_dup 源代码: 1 #incl ...