# 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; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up