LeetCode 1356. Sort Integers by The Number of 1 Bits
Question
Given an integer array arr
. You have to sort the integers in the array in ascending order by the number of 1’s in their binary representation and in case of two or more integers have the same number of 1’s you have to sort them in ascending order.
Return the sorted array.
Example
1 | Input: arr = [0,1,2,3,4,5,6,7,8] |
Solution
It’s a customized sorting problem. But we need to convert int
to Integer
, so that we can sort them.
Code
1 | // time: O(nlogn), space: O(n) |