11.连续数组
方法:前缀和+哈希表
class Solution {
public int findMaxLength(int[] nums) {
HashMap<Integer,Integer> map = new HashMap<>();
int ans = 0,n = nums.length;
int count = 0;
map.put(count,-1);
for(int i =0;i<n;i++){
int num = nums[i];
if(num==1){
count++;
}else{
count--;
}
if(map.containsKey(count)){
int pre = map.get(count);
ans = Math.max(ans,i - pre);
}else{
map.put(count,i);
}
}
return ans;
}
}