:type nums: List[int] :type k: int :rtype: int
(self, nums, k)
| 25 | """ |
| 26 | class Solution(object): |
| 27 | def subarraySum(self, nums, k): |
| 28 | """ |
| 29 | :type nums: List[int] |
| 30 | :type k: int |
| 31 | :rtype: int |
| 32 | """ |
| 33 | dicts = {0:1} |
| 34 | |
| 35 | result = 0 |
| 36 | |
| 37 | pre_sum = 0 |
| 38 | |
| 39 | for i in nums: |
| 40 | pre_sum += i |
| 41 | |
| 42 | result += dicts.get(pre_sum-k, 0) |
| 43 | |
| 44 | dicts[pre_sum] = dicts.get(pre_sum, 0) + 1 |
| 45 | |
| 46 | return result |
| 47 |