Advertisement
DeepRest

Subarray Sum Equals K

Feb 10th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. class Solution:
  2.     def subarraySum(self, nums: List[int], k: int) -> int:
  3.         ht = {0:1}
  4.         res = ps = 0
  5.         for i in nums:
  6.             ps += i  
  7.             res += ht.get(ps-k, 0)  
  8.             ht[ps] = ht.get(ps, 0) + 1
  9.         return res  
  10. #sliding window works for all positives or all negatives
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement