Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adafd39

Browse files
Update 1356.根据数字二进制下1的数目排序.md
增加Java实现
1 parent 6944147 commit adafd39

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎problems/1356.根据数字二进制下1的数目排序.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ public:
123123
## Java
124124
125125
```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+
}
126152
```
127153

128154

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /