Java O(1) space solution


  • 0
    M
    class Solution {
    public int findLength(int[] A, int[] B) {
        return Math.max(helper(A, B), helper(B, A));
    }
    
    private int helper(int[] A, int[] B) {
        int max = 0;
        for(int j = 0; j < B.length; j++) {
            for(int len = 0, i = 0, j2 = j; i < A.length && j2 < B.length; i++, j2++) {
                if(A[i] == B[j2])
                    max = Math.max(++len, max);
                else
                    len = 0;
            }
        }
        return max;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.