LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 1ms, memory 17.6MB, accepted 2025-12-30.

python
class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        return sorted(arr, key=lambda x: (bin(x).count('1'), x))

Source