|
| 1 | +<p>You are given an integer array <code>arr</code>. Sort the integers in the array in ascending order by the number of <code>1</code>'s in their binary representation and in case of two or more integers have the same number of <code>1</code>'s you have to sort them in ascending order.</p> |
| 2 | + |
| 3 | +<p>Return <em>the array after sorting it</em>.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | + |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> arr = [0,1,2,3,4,5,6,7,8] |
| 10 | +<strong>Output:</strong> [0,1,2,4,8,3,5,6,7] |
| 11 | +<strong>Explantion:</strong> [0] is the only integer with 0 bits. |
| 12 | +[1,2,4,8] all have 1 bit. |
| 13 | +[3,5,6] have 2 bits. |
| 14 | +[7] has 3 bits. |
| 15 | +The sorted array by bits is [0,1,2,4,8,3,5,6,7] |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:</strong> arr = [1024,512,256,128,64,32,16,8,4,2,1] |
| 22 | +<strong>Output:</strong> [1,2,4,8,16,32,64,128,256,512,1024] |
| 23 | +<strong>Explantion:</strong> All integers have 1 bit in the binary representation, you should just sort them in ascending order. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong>Constraints:</strong></p> |
| 28 | + |
| 29 | +<ul> |
| 30 | + <li><code>1 <= arr.length <= 500</code></li> |
| 31 | + <li><code>0 <= arr[i] <= 10<sup>4</sup></code></li> |
| 32 | +</ul> |
0 commit comments