-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution974.java
More file actions
32 lines (31 loc) · 1.06 KB
/
Solution974.java
File metadata and controls
32 lines (31 loc) · 1.06 KB
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
28
29
30
31
32
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class Solution {
public int subarraysDivByK(int[] A, int K) {
int[] tmp = new int[A.length + 1];
tmp[0] = 0;
for (int i = 1; i < A.length + 1; i++) {
tmp[i] = tmp[i - 1] + A[i - 1];
}
System.out.println(Arrays.toString(tmp));
Map<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < tmp.length; i++) {
if (hashMap.containsKey(((tmp[i] % K) + K) % K)) {
hashMap.put(((tmp[i] % K) + K) % K, hashMap.get(((tmp[i] % K) + K) % K) + 1);
} else {
hashMap.put(((tmp[i] % K) + K) % K, 1);
}
}
System.out.println(hashMap.keySet());
System.out.println(hashMap.values());
int ans = 0;
Iterator<Integer> iterator = hashMap.values().iterator();
while (iterator.hasNext()) {
int value = iterator.next();
ans += value * (value - 1) / 2;
}
return ans;
}
}