forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_974.java
More file actions
27 lines (22 loc) · 670 Bytes
/
_974.java
File metadata and controls
27 lines (22 loc) · 670 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _974 {
public static class Solution1 {
public int subarraysDivByK(int[] A, int K) {
int count = 0;
int sum = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int i = 0; i < A.length; i++) {
sum = (sum + A[i]) % K;
if (sum < 0) {
sum += K;
}
count += map.getOrDefault(sum, 0);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}
}
}