Merge pull request #863 from casnz1601/patch-20

Update 1356.根据数字二进制下1的数目排序.md
This commit is contained in:
程序员Carl
2021-10-26 10:29:38 +08:00
committed by GitHub

View File

@ -156,6 +156,17 @@ class Solution {
## 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