原题
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:
Input:nums = [1,1,1], k = 2
Output: 2
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].
Reference Answer
Method one
看了数组的长度,明显O(n^2)的时间复杂度会超时。这个时间复杂度一般只能用O(N)的解法了。
使用一个字典保存数组某个位置之前的数组和,然后遍历数组求和,这样当我们求到一个位置的和的时候,向前找sum-k是否在数组中,如果在的话,更新结果为之前的结果+1。同时,当前这个sum出现的次数就多了一次。
这个题的解法不难想出来,因为如果要降低时间复杂度,应该能想到增加空间复杂度,那么要么使用数组,要么就是用字典之类的,保留之前的结果。
时间复杂度是O(N),空间复杂度是O(N).
自己实现了一种O(n^2)的做法,时间复杂度太高,超时。参考答案最巧妙之处在于记录所有可能出现值的次数,而对于和为k的次数,只需要满足
if (sum - k) in d: res += d[sum - k]
即可求出所有可能次数。说白了就是用空间换时间,不过方法确实很巧妙!要注意学习!!!
Code
class Solution:
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
n = len(nums)
d = collections.defaultdict(int)
d[0] = 1
sum = 0
res = 0
for i in range(n):
sum += nums[i]
if sum - k in d:
res += d[sum - k]
d[sum] += 1
return res
Note
- 参考答案最巧妙之处在于记录所有可能出现值的次数,而对于和为k的次数,只需要满足
if (sum - k) in d: res += d[sum - k]
即可求出所有可能次数。说白了就是用空间换时间,不过方法确实很巧妙!要注意学习!!! - 注意
d = collections.defaultdict(int)
的使用,直接调用collections.defaultdic(int)
使得默认缺省key的value为0; - 这里初始值赋值为
d[0] = 1
,也可以算是动态规划+dict的默认初试配置了。
参考文献
[1] https://blog.csdn.net/fuxuemingzhu/article/details/82767119