class Solution {
public:
int findMaxLength(vector<int>& nums) {
int n = nums.size();
vector<int> sum(n+1);
for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + (nums[i-1] ? 1 : -1);
unordered_map<int,int> hash;
int res = 0;
hash[0] = 0;
for(int i = 1; i <= n; i++)
{
if(hash.find(sum[i]) != hash.end()) res = max(res, i - hash[sum[i]]);
else hash[sum[i]] = i;
}
return res;
}
};
相关文章
- 10-09【前缀和+Map】Leetcode560寻找等于K的子数组
- 10-09LeetCode 862. 和至少为 K 的最短子数组(前缀和+deque单调栈)
- 10-09LeetCode(26)连续的子数组和(中等)
- 10-09leetcode523. 连续的子数组和【M】
- 10-09LeetCode: 523. 连续的子数组和
- 10-09LeetCode#1013-将数组分成和相等的三个部分-前缀和-双指针
- 10-09Leetcode560. 和为K的子数组--前缀和
- 10-09leetcode525连续数组
- 10-09Leetcode 238. 除自身以外数组的乘积 前缀和
- 10-09Medium | LeetCode 560. 和为K的连续子数组 | 前缀和