C++


  • 0
    B
    class Solution {
    public:
        unordered_map<int, int> hashmap;
        int cnt = 0;
        int sum = 0;
        int subarraySum(vector<int>& nums, int k) {
            hashmap[0] = 1;
            for (int i=0; i < nums.size(); i++) {
                sum += nums[i];
                if (hashmap.find(sum-k) != hashmap.end()) {
                    cnt += hashmap[sum-k];
                }
                if (hashmap.find(sum) != hashmap.end()) {
                    hashmap[sum] += 1;
                }
                else {
                    hashmap[sum] = 1;
                }
            }
            return cnt;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.