力扣1546.和为目标值且不重叠的非空子数组的最大数目
-
从头开始找 找到满足条件的就清空哈希表
cpp
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
int n = nums.size();
vector<int> s(n+1);
for(int i=0;i<n;i++) s[i+1] = s[i] + nums[i];
unordered_map<int,int> cnt;
int res=0;
for(int sj:s)
{
if(cnt.find(sj - target) != cnt.end())
{
res ++;
cnt = unordered_map<int,int> {};
}
cnt[sj] ++;
}
return res;
}
};