class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int n=nums.size(),res=0;
vector<int> s(n+1,0);
unordered_map<int,int> hash;//记录端点i之前所有前缀和的出现情况
for(int i=1;i<=n;i++)
s[i]=s[i-1]+nums[i-1];
hash[0]=1;
for(int i=1;i<=n;i++)
{
res+=hash[s[i]-k];
hash[s[i]]++;
}
return res;
}
};
标签:nums,560,int,vector,数组,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17582172.html