给出的二进制数组的长度不会超过 50,000。public class Solution { /** * @param nums: a binary array * @return: the maximum length of a contiguous subarray */ public int findMaxLength(int[] nums) { int sum; int i1=0,i0=0; int max=0; HashMap hash=new HashMap<>(nums.length); hash.put(0,-1); for (int i=0; i if (nums[i]==0){ i0++; }else{ i1++; } sum=i1-i0; if (hash.containsKey(sum)){ int last=hash.get(sum); max=Math.max(max,i-last); }else{ hash.put(sum,i); } } return max; }}