We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6944147 commit adafd39Copy full SHA for adafd39
problems/1356.根据数字二进制下1的数目排序.md
@@ -123,6 +123,32 @@ public:
123
## Java
124
125
```java
126
+class Solution {
127
+ private int cntInt(int val){
128
+ int count = 0;
129
+ while(val > 0) {
130
+ val = val & (val - 1);
131
+ count ++;
132
+ }
133
+
134
+ return count;
135
136
137
+ public int[] sortByBits(int[] arr) {
138
+ return Arrays.stream(arr).boxed()
139
+ .sorted(new Comparator<Integer>(){
140
+ @Override
141
+ public int compare(Integer o1, Integer o2) {
142
+ // TODO Auto-generated method stub
143
+ int cnt1 = cntInt(o1);
144
+ int cnt2 = cntInt(o2);
145
+ return (cnt1 == cnt2) ? Integer.compare(o1, o2) : Integer.compare(cnt1, cnt2);
146
147
+ })
148
+ .mapToInt(Integer::intValue)
149
+ .toArray();
150
151
+}
152
```
153
154
0 commit comments