718. 最长重复子数组

给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。

示例:

输入:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
输出:3
解释:
长度最长的公共子数组是 [3, 2, 1] 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

动态规划

class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return 0;
        }
        int n = nums1.length;
        int m = nums2.length;
        int[][] dp = new int[n + 1][m + 1];
        int ret = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                ret = Math.max(ret, dp[i][j]);
            }
        }

        return ret;
    }
}

滑动窗口

class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return 0;
        }
        int ret = 0;
        int n = nums1.length, m = nums2.length;
        for (int i = 0; i < n; ++i) {
            int len = Math.min(m, n - i);
            ret = Math.max(ret, getMaxLength(nums1, i, nums2, 0, len));
        }

        for (int i = 0; i < m; ++i) {
            int len = Math.min(n, m - i);
            ret = Math.max(ret, getMaxLength(nums1, 0, nums2, i, len));
        }
        return ret;
    }

    private int getMaxLength(int[] nums1, int start1, int[] nums2, int start2, int length) {
        int ret = 0;
        int sum = 0;
        for (int i = 0; i < length; ++i) {
            if (nums1[start1 + i] == nums2[start2 + i]) {
                ++sum;
            } else {
                sum = 0;
            }
            ret = Math.max(ret, sum);
        }
        return ret;
    }
}

二分

import java.util.HashSet;
import java.util.Set;

class Solution {
    int mod = 1000000007;
    int base = 113;

    public int findLength(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return 0;
        }
        int n = nums1.length, m = nums2.length;
        int left = 0, right = Math.min(n, m);
        int ret = 0;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (check(nums1, nums2, mid)) {
                ret = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return ret;
    }

    private boolean check(int[] nums1, int[] nums2, int length) {
        Set<Long> set1 = new HashSet<>();
        long hash1 = 0;
        for (int i = 0; i < length; ++i) {
            hash1 = (hash1 * base + nums1[i]) % mod;
        }
        set1.add(hash1);
        long maxBase = quickPow(base, length - 1);
        for (int i = length; i < nums1.length; ++i) {
            hash1 = ((hash1 - nums1[i - length] * maxBase % mod + mod) * base % mod + nums1[i]) % mod;
            set1.add(hash1);
        }

        long hash2 = 0;
        for (int i = 0; i < length; ++i) {
            hash2 = (hash2 * base + nums2[i]) % mod;
        }
        if (set1.contains(hash2)) {
            return true;
        }

        for (int i = length; i < nums2.length; ++i) {
            hash2 = ((hash2 - nums2[i - length] * maxBase % mod + mod) * base % mod + nums2[i]) % mod;
            if (set1.contains(hash2)) {
                return true;
            }
        }

        return false;
    }

    private long quickPow(long x, int n) {
        long ret = 1;
        long base = x;
        while (n > 0) {
            if ((n & 1) == 1) {
                ret = (ret * base) % mod;
            }
            n >>= 1;
            base = (base * base) % mod;
        }
        return ret;
    }
}
上一篇:LeeCode-String to Integer (atoi)


下一篇:REST Framework 分页