-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathsubarraySum.py
38 lines (34 loc) · 908 Bytes
/
subarraySum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
time: n
space: n
Subarray Sum Equals K
1, -1, 0
rtn+=0:1 rtn+=0:2
0:2
"""
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
hm = collections.Counter([0])
rtn = ps = 0
for n in nums:
ps += n
rtn += hm[ps - k]
hm[ps] += 1
return rtn
def subarraySum(self, nums: List[int], k: int) -> int:
mp = {0: 1}
rtn, total = 0, 0
for n in nums:
total += n
rtn += mp.get(total - k, 0)
mp[total] = mp.get(total, 0) + 1
return rtn
def subarraySum(self, nums: List[int], k: int) -> int:
count = collections.Counter()
count[0] = 1
ans = total = 0
for x in nums:
total += x
ans += count[total-k]
count[total] += 1
return ans