Leetcode 974. Subarray Sums Divisible by K

前缀和(prefix sum/cumulative sum)的应用

class Solution(object):
    def subarraysDivByK(self, A, K):
        P = [0]
        for x in A:
            P.append((P[-1] + x) % K)

        count = collections.Counter(P)
        return sum(v*(v-1)/2 for v in count.values())

猜你喜欢

转载自www.cnblogs.com/zywscq/p/10544142.html