for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i]) + 1);
} else {
map.put(arr[i], 1);
}
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
pq.add(entry);
}
int point = 0;
while (!pq.isEmpty()) {
Map.Entry<Integer, Integer> entry = pq.poll();
int frequency = entry.getValue();
for (int i = 0; i < frequency; i++) {
arr[point] = entry.getKey();
point++;
}
}
}
第三题 我用的方法和你应该一样的 也是LeetCode答案
static long KSub(int k, int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int curMod = 0;
int[] modCount = new int[k];
for (int i = 0; i < nums.length; i++) {
curMod = (curMod + (nums[i] % k) + k) % k;
modCount[curMod]++;
}
int sum = 0;
for (int i = 1; i < modCount.length; i++) {
sum += (modCount[i] * (modCount[i] - 1)) / 2;
}
sum += (modCount[0] * (modCount[0] + 1)) / 2;
return sum;
}