classSolution(object):defsubarraySum(self, nums, k):"""
:type nums: List[int]
:type k: int
:rtype: int
"""
res =0hash= collections.defaultdict(int)hash[0]=1
pre_sum =0for num in nums:
pre_sum += num
#注意先 res += hash[pre_sum-k]再hash[pre_sum] += 1,不能将2者顺序交换,例如nums=[1],k=0
res +=hash[pre_sum-k]hash[pre_sum]+=1return res
classSolution:defproductExceptSelf(self, nums: List[int])-> List[int]:
n =len(nums)
res =[1]* n
tem =1for i inrange(1, n):
res[i]= nums[i-1]* res[i-1]for j inrange(n-2,-1,-1):
tem *= nums[j+1]
res[j]*= tem
return res