-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray-sum.py
More file actions
27 lines (18 loc) · 490 Bytes
/
subarray-sum.py
File metadata and controls
27 lines (18 loc) · 490 Bytes
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
import collections
from typing import Collection, List
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
cnt=0
consum=0
map=collections.defaultdict(int)
map[0]=1
for i in range(len(nums)):
consum+=nums[i]
if consum-k in map:
cnt+=map[consum-k]
map[consum]+=1
#print(consum,cnt)
return cnt
nums=[1,1,1]
k=2
print(Solution().subarraySum(nums,k))