560. 和为 K 的子数组
给你一个整数数组 nums 和一个整数 k ,请你统计并返回该数组中和为 k 的连续子数组的个数。
示例 1:
输入:nums = [1,1,1], k = 2
输出:2
示例 2:
输入:nums = [1,2,3], k = 3
输出:2
class Solution {
public int subarraySum(int[] nums, int k) {
int cnt = 0;
for (int start = 0; start < nums.length; ++start) {
int sum = 0;
for (int end = start; end >= 0; --end) {
sum += nums[end];
if (sum == k) {
++cnt;
}
}
}
return cnt;
}
}
class Solution {
public int subarraySum(int[] nums, int k) {
int cnt = 0, pre = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, 1);
for (int i = 0; i < nums.length; ++i) {
pre += nums[i];
if (map.containsKey(pre - k)) {
cnt += map.get(pre - k);
}
map.put(pre, map.getOrDefault(pre, 0) + 1);
}
return cnt;
}
}