1356. Sort Integers by The Number of 1 Bits
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 79% Topics: Array, Bit Manipulation, Sorting, Counting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def sortByBits(self, arr):
"""
:type arr: List[int]
:rtype: List[int]
"""
def popcount(n): # Time: O(logn) ~= O(1) if n is a 32-bit number
result = 0
while n:
n &= n - 1
result += 1
return result
arr.sort(key=lambda x: (popcount(x), x))
return arr
Solution from kamyu104/LeetCode-Solutions · MIT