leetcode560_SubarraySumEquaslK
作者:互联网
public int subarraySum(int[] nums, int k) {
int len = nums.length, ans = 0, pre = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0,1);
for(int i = 0; i < len; i++) {
pre += nums[i];
map.put(pre-k, map.get(pre-k)+1);
ans += map.get(pre-nums[i]);
}
return ans;
}
标签:pre,map,leetcode560,nums,int,SubarraySumEquaslK,Map 来源: https://www.cnblogs.com/huangming-zzz/p/15929592.html