class Solution {
public int findMaxLength(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int len = nums.length;
int res = 0;
int max = 0;
for(int i = 0; i < len; i++){
nums[i] = nums[i] == 1 ? 1 : -1;
}
for(int i = 0; i < len; i++){
res += nums[i];
if (map.containsKey(res)) {
int prevIndex = map.get(res);
max = Math.max(max, i - prevIndex);
} else {
map.put(res, i);
}
}
return max;
}
}
07-24
838
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)