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

新增python代码
This commit is contained in:
Asterisk
2021-10-21 11:13:08 +08:00
committed by GitHub
parent 77c1098ff3
commit 8451af1e78

View File

@ -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