-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1356.java
More file actions
66 lines (59 loc) · 2.15 KB
/
LeetCode1356.java
File metadata and controls
66 lines (59 loc) · 2.15 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
public class LeetCode1356 {
public static void main(String[] args) {
// 输入:arr = [0,1,2,3,4,5,6,7,8]
// 输出:[0,1,2,4,8,3,5,6,7]
System.out.println(Arrays.toString(new Solution1356().sortByBits(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 })));
// 输入:arr = [1024,512,256,128,64,32,16,8,4,2,1]
// 输出:[1,2,4,8,16,32,64,128,256,512,1024]
System.out.println(Arrays
.toString(new Solution1356().sortByBits(new int[] { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 })));
// 输入:arr = [10000,10000]
// 输出:[10000,10000]
System.out.println(Arrays
.toString(new Solution1356().sortByBits(new int[] { 10000, 10000 })));
// 输入:arr = [2,3,5,7,11,13,17,19]
// 输出:[2,3,5,17,7,11,13,19]
System.out.println(Arrays
.toString(new Solution1356().sortByBits(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 })));
// 输入:arr = [10,100,1000,10000]
// 输出:[10,100,10000,1000]
System.out.println(Arrays
.toString(new Solution1356().sortByBits(new int[] { 10, 100, 1000, 10000 })));
}
}
class Solution1356 {
public int[] sortByBits(int[] arr) {
int[] values = new int[10001];
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
values[arr[i]] = getValue(arr[i]);
}
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (values[o1] != values[o2]) {
return values[o1] - values[o2];
} else {
return o1 - o2;
}
}
});
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public int getValue(int x) {
int res = 0;
while (x != 0) {
res += x % 2;
x /= 2;
}
return res;
}
}