I have been trying to solve the "Subarray Sum Equals K" problem on leetcode. However I am not able to solve some test cases with the following code:
from collections import defaultdict
class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
sumTable = defaultdict(lambda : 0)
count = 0
totalSum = 0
for i in range(0,len(nums)):
totalSum += nums[i]
if(totalSum==k):
count += 1
sumTable[totalSum] += 1
for key in sumTable:
# print(key)
if (key-k) in sumTable:
count += sumTable[key-k]
return count