- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1356.java
32 lines (30 loc) · 970 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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclass_1356 {
publicstaticclassSolution1 {
publicint[] sortByBits(int[] arr) {
Map<Integer, List<Integer>> map = newHashMap<>();
for (intnum : arr) {
intcount = Integer.bitCount(num);
if (!map.containsKey(count)) {
map.put(count, newArrayList<>());
}
map.get(count).add(num);
}
int[] result = newint[arr.length];
inti = 0;
for (intcount : map.keySet()) {
List<Integer> list = map.get(count);
Collections.sort(list);
for (intnum : list) {
result[i++] = num;
}
}
returnresult;
}
}
}