C++ 9-line easy O(n) solution


  • 0
    M
        int subarraySum(vector<int>& nums, int k) {
            unordered_map<int, int> dict;
            dict[0]=1;
            int sum=0, res=0;
            for(int i=0;i<nums.size();i++) {
                sum+=nums[i];
                res+=dict[sum-k];
                dict[sum]++;
            }
            return res;
        }

Log in to reply
 

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