Skip to content

Commit 31a1cad

Browse files
authored
Create ContiguousArray.java
1 parent 23d8708 commit 31a1cad

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ContiguousArray.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int findMaxLength(int[] nums) {
3+
4+
Map<Integer, Integer> indexes = new HashMap<>();
5+
indexes.put(0, -1);
6+
7+
int maxlen = 0, count = 0;
8+
9+
for (int i = 0; i < nums.length; i++) {
10+
11+
count = count + (nums[i] == 1 ? 1 : -1);
12+
13+
if (indexes.containsKey(count)) {
14+
maxlen = Math.max(maxlen, i - indexes.get(count));
15+
} else {
16+
indexes.put(count, i);
17+
}
18+
}
19+
20+
return maxlen;
21+
}
22+
}

0 commit comments

Comments
 (0)