package leetcode;

/**
 * https://leetcode.com/problems/contiguous-array/
 */
public class Problem525 {
    public int findMaxLength(int[] nums) {
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            int zero = 0;
            int one = 0;
            for (int j = i; j < nums.length; j ++) {
                if (nums[j] == 0) {
                    zero++;
                } else {
                    one++;
                }
                if (zero == one) {
                    max = Math.max(max, zero + one);
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Problem525 prob = new Problem525();
        System.out.println(prob.findMaxLength(new int[]{0, 1})); // 2
        System.out.println(prob.findMaxLength(new int[]{0, 1, 0})); // 2
        System.out.println(prob.findMaxLength(new int[]{0, 1, 1, 0, 1})); // 4
        System.out.println(prob.findMaxLength(new int[]{1, 1, 1, 0, 0})); // 4
        System.out.println(prob.findMaxLength(new int[]{1, 1, 1, 1, 1})); // 0
        System.out.println(prob.findMaxLength(new int[]{1, 1, 0, 0, 0, 0, 1})); // 4
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -222,78 +222,79 @@
     
-for (int i = 0; i %3C nums.length; i++) %7B%0A            int zero = 0;%0A    
+// count -%3E index%0A        Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A
     
@@ -305,21 +305,19 @@
 int 
-one
+count
  = 0;%0A
-    
     
@@ -333,16 +333,16 @@
 int 
-j = i; j
+i = 0; i
  %3C n
@@ -357,10 +357,9 @@
 th; 
-j 
+i
 ++) 
@@ -368,28 +368,24 @@
             
-    
 if (nums%5Bj%5D 
@@ -381,17 +381,17 @@
 f (nums%5B
-j
+i
 %5D == 0) 
@@ -412,24 +412,17 @@
     
-    zero++;%0A    
+count--;%0A
     
@@ -458,15 +458,13 @@
     
-    one
+count
 ++;%0A
@@ -471,32 +471,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -493,33 +493,26 @@
     
-  
 if (
-zero
+count
  == 
-one
+0
 ) %7B%0A
-    
     
@@ -547,18 +547,13 @@
 ax, 
-zero + one
+i + 1
 );%0A 
@@ -567,27 +567,16 @@
     
-    %7D%0A            %7D
+%7D else %7B
 %0A   
@@ -580,26 +580,24 @@
         
-%7D%0A
         
 return m
@@ -592,580 +592,224 @@
     
-return max;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem525 prob = new Problem525();%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B0, 1%7D)); // 2%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B0, 1, 0%7D)); // 2%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B0, 1, 1, 0, 1%7D)); // 4%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B1, 1, 1, 0, 0%7D)); // 4%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B1, 1, 1, 1, 1%7D)); // 0%0A        System.out.println(prob.findMaxLength(new int%5B%5D%7B1, 1, 0, 0, 0, 0, 1%7D)); // 4
+if (map.containsKey(count)) %7B%0A                    max = Math.max(max, i - map.get(count));%0A                %7D else %7B%0A                    map.put(count, i);%0A                %7D%0A            %7D%0A        %7D%0A        return max;
 %0A   

Solve problem 525