Subarray Sum Equals K(2sum and presum)
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:
Note:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
分析:
本题只能two sum思想,不能用two pointers,因为可能有负数,所以当前的sum可能和前面好几个sum的diff == K. 用Map才能准确记录前面多少个subsum = cursum - K.(含有负数抵消的情况)
这里没有presum数组,只有sum累积
这里map存个数 map[sum]=count
对map和ret都是+=1不是=!!!!!
PYTHON MAP:MAP.GET(KEY,0)
Last updated