Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
subarray-sums-divisible-by-k.py 444 Bytes
Copy Edit Raw Blame History
kamyu authored 2019-01-13 16:22 +08:00 . Create subarray-sums-divisible-by-k.py
# Time: O(n)
# Space: O(k)
import collections
class Solution(object):
def subarraysDivByK(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
count = collections.defaultdict(int)
count[0] = 1
result, prefix = 0, 0
for a in A:
prefix = (prefix+a) % K
result += count[prefix]
count[prefix] += 1
return result
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search