974. Subarray Sums Divisible by K
跟昨天差不多
負數好像也可以直接%
之前都沒關注負數的mod
學到了
def subarraysDivByK(self, nums: List[int], k: int) -> int:
cnt = defaultdict(int)
cnt[0] = 1
ans, sum_cur = 0, 0
for num in nums:
sum_cur += num
ans += cnt[sum_cur%k]
cnt[sum_cur%k] += 1
return ans