Subarray sum equals K - Prefix Sum problem
Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.
A subarray is a contiguous non-empty sequence of elements within an array.
class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
count = 0
sums = 0
d = dict()
d[0] = 1
for i in range(len(nums)):
sums += nums[i]
count += d.get(sums-k,0)
d[sums] = d.get(sums,0) + 1
return(count)
Posted by Jamie Meyer 21 days ago