python - 70ms - running sum


  • 0
    S
    class Solution(object):
        def subarraySum(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: int
            """
            # compute the running sum
            RS, ans, d = 0,0,{}
    
            for i in nums:
                RS += i
                ans += d.get(RS-k,0) + (1 if RS == k else 0)
                d[RS] = d.get(RS, 0) + 1
            
            return ans
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.