1458. Sort Integers by The Number of 1 Bits
My accepted Python solution to LeetCode problem 1458, Sort Integers by The Number of 1 Bits, running in 1ms.
- Difficulty: Easy
- Python
- Runtime 1ms
- Memory 17.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1ms, memory 17.6MB, accepted 2025-12-30.
class Solution:
def sortByBits(self, arr: List[int]) -> List[int]:
return sorted(arr, key=lambda x: (bin(x).count('1'), x))