Skip to content

Commit 150e5d9

Browse files
committed
525
1 parent 6921330 commit 150e5d9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

525.continuous_array.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//coding:utf-8
2+
/***********************************************************
3+
Program:
4+
Description: Check the sum so far
5+
Shanbo Cheng: [email protected]
6+
Date: 2017-05-15 23:27:47
7+
Last modified: 2017-05-15 23:28:23
8+
GCC version: 4.9.3
9+
***********************************************************/
10+
11+
class Solution {
12+
public:
13+
int findMaxLength(vector<int>& nums) {
14+
int cnt = 0, max_len = 0;
15+
unordered_map<int, int> map;
16+
map[0] = -1;
17+
for(int i = 0; i < nums.size(); ++i) {
18+
cnt += (nums[i] == 0?-1: 1);
19+
if(map.find(cnt) == map.end())
20+
map[cnt] = i;
21+
else
22+
max_len = max(max_len, i - map[cnt]);
23+
}
24+
return max_len;
25+
}
26+
};
27+

0 commit comments

Comments
 (0)