Skip to content

Commit 820bb27

Browse files
authored
Create Solution.java
1 parent 25b8eb7 commit 820bb27

File tree

1 file changed

+17
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)