Skip to content

Instantly share code, notes, and snippets.

@chelsea1992
Created April 16, 2017 01:51
Show Gist options
  • Save chelsea1992/1becdc9dd3b0a816170c37862c531555 to your computer and use it in GitHub Desktop.
Save chelsea1992/1becdc9dd3b0a816170c37862c531555 to your computer and use it in GitHub Desktop.
LeetCode,525. Contiguous Array
public class Solution {
public int findMaxLength(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) nums[i] = -1;
}
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0, max = 0;
for(int i = 0; i<nums.length; i++){
sum += nums[i];
if(map.containsKey(sum)){
max = Math.max(max,i - map.get(sum));
}
else{
map.put(sum,i);
}
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment