Skip to content

Commit bdc14ae

Browse files
committed
add cpp solution
1 parent a5d4ff2 commit bdc14ae

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
int n = nums.size();
5+
int sum = 0;
6+
map<int, int> min_index;
7+
8+
int ans = 0;
9+
for (int i = 0; i < n; i++){
10+
sum += (nums[i] == 0)?-1:1;
11+
if (sum == 0) ans = i+1;
12+
else if (min_index.find(sum) == min_index.end())
13+
min_index[sum] = i;
14+
else ans = max(ans, i - min_index[sum]);
15+
}
16+
return ans;
17+
}
18+
};
19+

0 commit comments

Comments
 (0)