leetcode 718

最长重复子数组

Posted by Static on July 1, 2020

题目链接:https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/

题目描述


分析

1. 双循环

时间负责度过高,O(N^2)

2. 滑动窗口


代码实现

public enum Q718 {
    instance;

    // 双循环,时间复杂度过大
    public int findLength(int[] A, int[] B) {
        int maxCommonLength=0;
        for(int i=0;i<A.length;i++){
                int p=i,j=0,q=j,length=0;
                for(;j<B.length&&q<B.length&&p<A.length;q++){
                    if(A[p]!=B[q]&&length>0){
                        if(length>maxCommonLength){
                            maxCommonLength=length;
                        }
                        length=0;
                        p=i;
                        q=j++;
                    }else if(A[p]==B[q]){
                        p++;
                        length++;
                    }
            }
            // 对于A尾部与B子数组相等时
            if(length>maxCommonLength){
                maxCommonLength=length;
            }
        }
        return maxCommonLength;
    }

    // 滑动窗口,O(m+n)
    public int findLength1(int[] A, int[] B) {
        int m = A.length, n = B.length, res = 0;
        for (int diff = -(m - 1); diff <= n - 1; ++diff) { // 枚举对应关系
            for (int i = Math.max(0, -diff), l = 0; i < Math.min(m, n - diff); ++i) { // 遍历公共部分
                l = (A[i] == B[i + diff]) ? (l + 1) : 0;
                res = Math.max(res, l);
            }
        }
        return res;
    }


    public static void main(String[] args) {
        // assert 3
        System.out.println(Q718.instance.findLength(new int[]{1,2,3,2,1},new int[]{3,2,1,4,7}));
        // assert 6
        System.out.println(Q718.instance.findLength(new int[]{1,0,1,0,0,0,0,0,1,1},new int[]{1,1,0,1,1,0,0,0,0,0}));
        // assert 9
        System.out.println(Q718.instance.findLength(new int[]{0,0,0,0,0,0,1,0,0,0},new int[]{0,0,0,0,0,0,0,1,0,0}));
        System.out.println(Q718.instance.findLength1(new int[]{0,0,0,0,0,0,1,0,0,0},new int[]{0,0,0,0,0,0,0,1,0,0}));
    }
}

Arrogance,jealously and greed are three sparks.They can explode the heart. (Italy) Dante

骄傲、嫉妒、贪婪是三个火星,它们使人心爆炸。 – [意大利] 但丁