LeetCode 718. 最长重复子数组

题目描述

🔥 718. 最长重复子数组

思路分析

dp[i][j] 表示 nums1 的前 i 项与 nums2 的前 j 项的最长重复子数组长度

参考代码

1
write your code here

🍏 点击查看 Java 题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        int m = nums1.length, n = nums2.length;
        int[][] dp = new int[m + 1][n + 1]; // dp[i][j] 表示以 nums1[i] 和 nums2[j] 结尾的两个子数组的最长公共子数组的长度
        int res = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    res = Math.max(res, dp[i][j]);
                }
            }
        }
        return res;
    }
}

相似题目

题目 难度 题解
长度最小的子数组 Medium  
本文作者:
本文链接: https://hgnulb.github.io/blog/15544319
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!