mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -156,6 +156,17 @@ class Solution {
|
|||||||
## Python
|
## Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
class Solution:
|
||||||
|
def sortByBits(self, arr: List[int]) -> List[int]:
|
||||||
|
arr.sort(key=lambda num: (self.count_bits(num), num))
|
||||||
|
return arr
|
||||||
|
|
||||||
|
def count_bits(self, num: int) -> int:
|
||||||
|
count = 0
|
||||||
|
while num:
|
||||||
|
num &= num - 1
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
Reference in New Issue
Block a user