Java solution without HashMap, beat 99.68%


  • 0
    Z
    public int findMaxLength(int[] nums) {
        int[] map = new int[2 * nums.length + 1];
        int dif = nums.length, i = 0, res = 0, len = nums.length;
        map[dif] = 0;
        for (int n: nums) {
            dif += n == 1 ? 1 : -1;
            i++;
            if (dif == len) res = Math.max(res, i);
            else if (map[dif] == 0) map[dif] = i;
            else res = Math.max(res, i - map[dif]);
        }
        return res;
    }
    

Log in to reply
 

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