当前位置:网站首页>LeetCode_ Dynamic programming_ Medium_ 718. Longest repeating subarray
LeetCode_ Dynamic programming_ Medium_ 718. Longest repeating subarray
2022-07-18 04:44:00 【Old street of small town】
1. subject
Give two arrays of integers nums1 and nums2 , Returns the common of two arrays 、 The length of the longest subarray .
Example 1:
Input :nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output :3
explain : The longest common subarray is [3,2,1] .
Example 2:
Input :nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output :5
Tips :
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 100
source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-length-of-repeated-subarray
2. Ideas
(1) The law of violent exhaustion
This method is easy to think of , namely Exhaustive array nums1 Starting position in i And an array nums2 Starting position in j, And then calculate nums1[i…length1 - 1] and nums2[j…length2 - 1] The longest public prefix of k, Use variables in the calculation process res Record k The maximum of , Return after exhausting res that will do . The time complexity of this method is O(n3).
(2) Dynamic programming
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— The law of violent exhaustion
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int res = 0;
int length1 = nums1.length;
int length2 = nums2.length;
for (int i = 0; i < length1; i++) {
for (int j = 0; j < length2; j++) {
int k = 0;
while (i + k < length1 && j + k <length2 && nums1[i + k] == nums2[j + k]) {
k++;
}
res = Math.max(res, k);
}
}
return res;
}
}
// Ideas 2———— Dynamic programming
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int res = 0;
int length1 = nums1.length;
int length2 = nums2.length;
//dp[i][j] Express nums1[i...length1 - 1] and nums2[j...length2 - 1] The longest public prefix of
int[][] dp = new int[length1 + 1][length2 + 1];
for (int i = length1 - 1; i >= 0; i--) {
for (int j = length2 - 1; j >=0; j--) {
dp[i][j] = (nums1[i] == nums2[j]) ? dp[i + 1][j + 1] + 1 : 0;
res = Math.max(dp[i][j], res);
}
}
return res;
}
}
边栏推荐
- VirtualBox:设置共享文件夹
- Machine learning exercise 4 - neural networks
- Link script of u-boot
- Rename file with command line
- 面试官:抽象工厂模式是什么?
- 2022年正规期货交易平台的app有哪些,安全吗?
- EN 1155建筑五金件摆动门开启装置—CE认证
- The nature and usage of binary search tree
- LeetCode_滑动窗口_二分搜索_中等_713.乘积小于 K 的子数组
- Cyclic multi-Variate Function for Self-Supervised Image Denoising by Disentangling Noise from Image
猜你喜欢

【详细教程】一文参透MongoDB聚合查询

Cookies and sessions

对于测试BUG的处理,代码缺陷的管理和改进

一种嵌入式中应用层与硬件层分层管理方法

OKX 金融市场总监 Lennix Lai:Web3 是一场革命

RS2022/云检测:考虑域偏移问题的卫星图像半监督云检测Semi-Supervised Cloud Detection in Satellite Images by Considering the

718. Longest repeating subarray

1301_ Two ways to add serial port monitoring function to the development board

SSM整合

SwiftUI视图onReceive方法接收“冗余”事件的解决
随机推荐
版本通告|Apache Doris 1.1 Release 版本正式发布!
机器学习练习 4 - 神经网络
Send your code into space and develop "the greatest work" with Huawei cloud
scrollIntoView实现简单的锚点定位(示例:选择城市列表)
Winform屏幕截图保存C#代码
【福利活动】给你的代码叠个 Buff!点击“茶”收好礼
1035. Disjoint lines
For the treatment of test bugs, the management and improvement of code defects
红外超分调研
C#接口知识大全收藏建议收藏
一种新的UI测试方法:视觉感知测试
Une nouvelle méthode de test d'interface utilisateur: test de perception visuelle
Pat grade a a1043 is it a binary search tree
log4j.properties 日志详解
VirtualBox:设置共享文件夹
Properties and traversal of binary trees
eureka server剖析
2022年宁德市职业院校教师实践教学能力提升培训——网络搭建与管理
Generate XML file of VOC dataset
The nature and usage of binary search tree