Skip to content

Instantly share code, notes, and snippets.

@EXJUSTICE
Created February 14, 2020 23:45
Show Gist options
  • Save EXJUSTICE/b25b99a57889e864f76eb718d4bd961c to your computer and use it in GitHub Desktop.
Save EXJUSTICE/b25b99a57889e864f76eb718d4bd961c to your computer and use it in GitHub Desktop.
def subarraySum(self, nums: 'List[int]', k: 'int') -> 'int':
count = 0
dictionary = collections.defaultdict(int)
current_sum = 0
#Basic enumeration
for index, val in enumerate(nums):
current_sum += val
#if match detected increment count
if current_sum == k:
count += 1
if current_sum - k in dictionary:
count += dictionary[current_sum - k]
dictionary[current_sum] += 1
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment