Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 34701cf

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 1d5ec1a + 634db9d commit 34701cf

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ class Solution {
156156
## Python
157157

158158
```python
159+
class Solution:
160+
def sortByBits(self, arr: List[int]) -> List[int]:
161+
arr.sort(key=lambda num: (self.count_bits(num), num))
162+
return arr
163+
164+
def count_bits(self, num: int) -> int:
165+
count = 0
166+
while num:
167+
num &= num - 1
168+
count += 1
169+
return count
159170
```
160171

161172
## Go

0 commit comments

Comments
 (0)