Leetcode 560 Solution

This article provides solution to leetcode question 560 (subarray-sum-equals-k)

https://leetcode.com/problems/subarray-sum-equals-k

Solution

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int64_t, int> m;

        m[0] = 1;

        int64_t s = 0;
        int total = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            s += nums[i];

            if (m.find(s - k) != m.end())
                total += m[s - k];

            m[s]++;
        }

        return total;
    }
};