Skip to content

Commit 034bf9b

Browse files
committed
add subarray sum = k python
1 parent be88d00 commit 034bf9b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/Subarray_Sum_Equals_K.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def subarraySum(self, nums, k):
2+
"""
3+
:type nums: List[int]
4+
:type k: int
5+
:rtype: int
6+
"""
7+
countMap = {0: 1}
8+
current = 0
9+
result = 0
10+
for i in nums:
11+
current += i
12+
result += countMap[current - k] if (current - k) in countMap else 0
13+
countMap[current] = countMap.get(current, 0) + 1
14+
print(countMap)
15+
return result

0 commit comments

Comments
 (0)