1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: int subarraySum(vector<int>& nums, int k) { unordered_map<int, int> map; map[0] = 1; int count = 0; int curSum = 0; for(auto& num : nums) { curSum += num; if(map.find(curSum - k) != map.end()) count += map[curSum - k]; map[curSum] += 1; } return count; } };
|