Leetcode 525. Contiguous Array
2018-09-12 本文已影响2人
SnailTyan
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Contiguous Array2. Solution
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int length = 0;
int sum = 0;
unordered_map<int, int> m({{0, -1}});
for(int i = 0; i < nums.size(); i++) {
sum += nums[i]?1:-1;
if(m.count(sum)) {
length = max(length, i - m[sum]);
}
else {
m[sum] = i;
}
}
return length;
}
};