当前位置:网站首页>Leetcode 560. And is the prefix and of the subarray of K

Leetcode 560. And is the prefix and of the subarray of K

2022-06-09 02:40:00 Confident little screw

Original link :Leetcode 560. And for K Subarray
 Insert picture description here

class Solution {
    
public:
    int subarraySum(vector<int>& nums, int k) {
    
        int n=nums.size(),res=0,tmp=0;
        unordered_map<int,int> mp;
        mp[0]=1;
        for(int i=0;i<n;i++)
        {
    
            tmp+=nums[i];
            if(mp.count(tmp-k)) res+=mp[tmp-k];
            mp[tmp]++;
        }
        return res;
    }
};
原网站

版权声明
本文为[Confident little screw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081210205820.html