0
点赞
收藏
分享

微信扫一扫

[leetcode] 560. Subarray Sum Equals K


Description

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:
Input:

nums = [1,1,1], k = 2

Output:

2

Note:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

分析

题目的意思是:找出一个数组里面连续子数组的和为K的子数组的个数。

  • 用一个map存起来,mp[0]的初始值为1,用于然后mp记录的是前m项的求和,mp[sum-k]如果为1,表明存在一个连续子序列的值为k,这需要找个test case模拟一下就行了。

代码

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int sum=0;
map<int,int> mp;
int count=0;
mp[0]++;
for(int i=0;i<nums.size();i++){
sum+=nums[i];
count+=mp[sum-k];
mp[sum]++;
}
return count;
}
};

参考文献

​​LeetCode Subarray Sum Equals K 子数组和为K​​​​560. Subarray Sum Equals K​​


举报

相关推荐

0 条评论