c++ solution


  • 0
    L
    int subarraySum(vector<int>& nums, int k) {
        int sum = 0, res = 0;
        unordered_map<int, int> hm;   // <preSum, times>     
        for (int x : nums) {
            ++hm[sum];
            sum += x;
            res += hm[sum-k];
        }   
        return res;
    }

Log in to reply
 

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