LeetCode 560. 和为K的子数组

给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。

示例 1 :

输入:nums = [1,1,1], k = 2
输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。

说明 :

    数组的长度为 [1, 20,000]。
    数组中元素的范围是 [-1000, 1000] ,且整数 k 的范围是 [-1e7, 1e7]。
算法:哈希表

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int,int>hash;
        int res=0,tot=0;
        hash[0]=1;
        for(auto x:nums){
            tot+=x;
            res+=hash[tot-k];
            hash[tot]++;
        }
        return res;
    }
};

 

posted @ 2019-07-09 15:52  YF-1994  阅读(151)  评论(0)    收藏  举报