Created
April 16, 2017 01:51
-
-
Save chelsea1992/1becdc9dd3b0a816170c37862c531555 to your computer and use it in GitHub Desktop.
LeetCode,525. Contiguous Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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