Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit caac3f5

Browse files
committed
974 finish
1 parent 6436f4f commit caac3f5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package problem0974
22

33
func subarraysDivByK(A []int, K int) int {
4-
5-
return 0
4+
count := [10001]int{}
5+
count[0] = 1
6+
prefix, res := 0, 0
7+
for _, a := range A {
8+
prefix = (prefix + a%K + K) % K
9+
res += count[prefix] // 减去相同的 prefix,剩下的就都是 K 的倍数
10+
count[prefix]++
11+
}
12+
return res
613
}

Algorithms/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k_test.go

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)