首页 > 其他分享 >【LeeCode】718. 最长重复子数组

【LeeCode】718. 最长重复子数组

时间:2023-02-18 10:31:56浏览次数:60  
标签:length 718 int res 重复子 LeeCode new nums1 nums2

【题目描述】

给两个整数数组 ​​nums1​​ 和 ​​nums2​​ 两个数组中 公共的 、长度最长的子数组的长度 。

https://leetcode.cn/problems/maximum-length-of-repeated-subarray/description/


【示例】

【LeeCode】718. 最长重复子数组_数组

【代码】



【代码】admin

通过率:  16/53

思路:暴力PO解, 2个for循环, 如果匹配到相等, 则开始依次遍历后面的 (如果遇到2个相同的数组,则直接返回)

package com.company;

import javax.swing.plaf.IconUIResource;
import java.util.*;

// 2022-02-18
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int len = nums1.length;
int len2 = nums2.length;
int[] min;
int[] max;
if (len == len2) {
min = nums1;
max = nums2;
}else{
min = len < len2 ? nums1 : nums2;
max = len < len2 ? nums2 : nums1;
}
int res = 0;

for (int i = 0; i < min.length; i++){
int count = 0;
for (int j = 0; j < max.length; j++){
if (min[i] == max[j]){
int left = i;
int right = j;
while (left < min.length && right < max.length){
if (min[left] == max[right]){
count++;
left++;
right++;
}else{
break;
}
}
if (res == len && len == len2) return len;
res = Math.max(count, res);
}
}
}
System.out.println(res);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().findLength(new int[]{1,2,3,2,1}, new int[]{3,2,1,4,7}); // 输出: 3
new Solution().findLength(new int[]{0,0,0,0,0}, new int[]{0,0,0,0,0}); // 输出: 5
}
}


【代码】admin2

用例100%通过;

思路: 在第一版基础上,参考​​官网的暴PO思路​​ ( 引入第3个变量k, 如果 nums1[i + k] == nums2[j + k] 则count++)

package com.company;
import java.util.*;

// 2022-02-18
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int res = 0;

for(int i = 0; i < nums1.length; i++){
for (int j = 0; j < nums2.length; j++){
int k = 0;
// i+k 和 j+k是为了控制循环不越界
while (i + k < nums1.length && j + k < nums2.length && nums1[i + k] == nums2[j + k]){
k++;
}
res = Math.max(res, k);
}
}
System.out.println(res);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().findLength(new int[]{1,2,3,2,1}, new int[]{3,2,1,4,7}); // 输出: 3
new Solution().findLength(new int[]{0,0,0,0,0}, new int[]{0,0,0,0,0}); // 输出: 5
}
}


【代码】​​动态规划​

package com.company;
import java.util.*;

// 2022-02-18
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int res = 0;
int len = nums1.length;
int len2 = nums2.length;
// 如果最后一个元素相同, 则有至少一个公共元素 所以数组长度+1
int[][] dp = new int[len+1][len2+1];
// 如果 2个数组的第一个数字相同, 则数组中第二个数字的公共前缀+1
// nums1[i] == nums2[j] ? 则
for (int i = len - 1; i >= 0; i--){
for (int j = len2 - 1; j >= 0; j--){
dp[i][j] = nums1[i] == nums2[j] ? dp[i + 1][j + 1] + 1 : 0;
res = Math.max(res, dp[i][j]);
}
}
System.out.println(res);
return res;
}
}

public class Test {
public static void main(String[] args) {
new Solution().findLength(new int[]{1,2,3,2,1}, new int[]{3,2,1,4,7}); // 输出: 3
new Solution().findLength(new int[]{0,0,0,0,0}, new int[]{0,0,0,0,0}); // 输出: 5
}
}


标签:length,718,int,res,重复子,LeeCode,new,nums1,nums2
From: https://blog.51cto.com/u_13682316/6065112

相关文章

  • 【LeeCode】724. 寻找数组的中心索引
    【题目描述】给你一个整数数组 ​​nums​​ ,请计算数组的 中心下标 。数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。如果中心下......
  • 【LeeCode】739. 每日温度
    【题目描述】给定一个整数数组 ​​temperatures​​ ,表示每天的温度,返回一个数组 ​​answer​​ ,其中 ​​answer[i]​​ ​​i​​ 天,下一个更高温度出现在几天后......
  • 【LeeCode】581. 最短无序连续子数组
    LeeCode【题目描述】给你一个整数数组 ​​nums​​ ,你需要找出一个 连续子数组 ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。请你找出符合题意的 最......
  • 【LeetCode字符串#06】KMP巩固练习:重复子串
    重复的子字符串力扣题目链接(opensnewwindow)给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。......
  • AcWing 799. 最长连续不重复子序列
    (双指针算法优化)思路:暴力解法:for(inti=0;i<n;i++)for(intj=0;j<=i;j++)check(i,j);算法优化:找到某种性质,尤其注意解题过程中存在的......
  • 718~719 HTTP响应消息响应头 AND Response功能介绍
    HTTP协议:1.请求消息:客户端发送给服务端的数据数据格式:1.请求行2.请求头3.请求空行4.请求体2.响应消息......
  • 718~719 HTTP响应消息响应头 AND Response功能介绍
    HTTP协议:1.请求消息:客户端发送给服务端的数据数据格式:1.请求行2.请求头3.请求空行4.请求体2.响应消息:......
  • 【LeeCode】131. 分割回文串
    【题目描述】给你一个字符串 ​​s​​,请你将 ​​s​​ 分割成一些子串,使每个子串都是 回文串 。返回 ​​s​​ 所有可能的分割方案。回文串 是正着读和反着读都......
  • 【LeeCode】131. 分割回文串 -- 异常
    【题目描述】给你一个字符串 ​​s​​,请你将 ​​s​​ 分割成一些子串,使每个子串都是 回文串 。返回 ​​s​​ 所有可能的分割方案。回文串 是正着读和反着读都......
  • 【LeeCode】215. 数组中的第K个最大元素
    【题目描述】给定整数数组 ​​nums​​​ 和整数 ​​k​​​,请返回数组中第 ​​k​​ 个最大的元素。请注意,你需要找的是数组排序后的第 ​​k​​ 个最大的元素,......