---
# System prepended metadata

title: 560-Subarray Sum Equals K
tags: [Medium]

---

# 560-Subarray Sum Equals K
###### tags: `Medium`
## Question
https://leetcode.com/problems/subarray-sum-equals-k/
## Key
1. Two sum延伸 (Hash table)
2. prefix + hash table解
## Reference
## Solution
```cpp=
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
            unordered_map<int,int> mp;
            int sum=0,ans=0;
            mp[sum] = 1;
            for(auto it:nums){
                sum += it;
                int find = sum - k;
                if(mp.find(find) != mp.end()){
                    ans += mp[find];
                }
                mp[sum]++;
            }
            return ans;
        }
};
```
