560 Subarray Sum Equals K
Given an array of integers nums
and an integer k
, return the total number of subarrays whose sum equals to k
.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
1 <= nums.length <= 2 * 104
-1000 <= nums[i] <= 1000
-10^7 <= k <= 10^7
好像这类比较难直接slide window的subarray都可以用L138 Subarray Sum的方法解决。138找的是equal 0,这里找到是equal k。这里需要注意的一点是,我们要用map而不是set。因为我们要记录等于某一个prefixsum的数目,然后加上。因为nums里有正有负,可能有重复的prefixsum。不加数目到map里的话,会漏掉答案的。T:O(n), S:O(n)
Last updated