forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1356.java
32 lines (30 loc) · 955 Bytes
/
_1356.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _1356 {
public static class Solution1 {
public int[] sortByBits(int[] arr) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int num : arr) {
int count = Integer.bitCount(num);
if (!map.containsKey(count)) {
map.put(count, new ArrayList<>());
}
map.get(count).add(num);
}
int[] result = new int[arr.length];
int i = 0;
for (int count : map.keySet()) {
List<Integer> list = map.get(count);
Collections.sort(list);
for (int num : list) {
result[i++] = num;
}
}
return result;
}
}
}