-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path525-连续数组.cpp
More file actions
25 lines (23 loc) · 806 Bytes
/
525-连续数组.cpp
File metadata and controls
25 lines (23 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int findMaxLength(vector<int>& nums) {
//先把所有的0都换成-1,这样题目变成求和为0的最长连续子数组
//sum[i] = nums[1] + nums[2] +...+nums[i], sum[j] = nums[1] + nums[2] +...+nums[j], i < j
//如果nums[i] = nums[j], 说明nums[i+1] +...+ nums[j] = 0
//以<累加和,下标>建hash表
unordered_map<int, int> hash;
hash[0] = -1;
int sum = 0, maxLen = 0;
for(int i=0;i<nums.size();i++)
{
if(nums[i] == 0)
nums[i] = -1;
sum += nums[i];
if(hash.find(sum) != hash.end())
maxLen = max(maxLen, i - hash[sum]);
else
hash[sum] = i;
}
return maxLen;
}
};