Sort by Bit Count
Return the numbers of nums sorted as a new list so that numbers with fewer 1 bits in their binary form come first. When two numbers have the same count of 1 bits, the smaller number comes first. Equal numbers are identical, so their order among themselves does not matter. For an empty nums return an empty list.
Example 1
nums [3,4,1,8,7]
returns [1,4,8,3,7]
1, 4 and 8 have one 1 bit each and are sorted by value; 3 has two 1 bits and 7 has three.
Example 2
nums [0,1000,5]
returns [0,5,1000]
0 has no 1 bits, 5 (101) has two and 1000 has six.
Constraints
0 <= len(nums) <= 1000 <= nums[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.