java.lang.Object
g1301_1400.s1356_sort_integers_by_the_number_of_1_bits.Solution

public class Solution extends Object
1356 - Sort Integers by The Number of 1 Bits.<p>Easy</p> <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>&rsquo;s in their binary representation and in case of two or more integers have the same number of <code>1</code>&rsquo;s you have to sort them in ascending order.</p> <p>Return <em>the array after sorting it</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> arr = [0,1,2,3,4,5,6,7,8]</p> <p><strong>Output:</strong> [0,1,2,4,8,3,5,6,7] <strong>Explantion:</strong> [0] is the only integer with 0 bits.</p> <p>[1,2,4,8] all have 1 bit.</p> <p>[3,5,6] have 2 bits.</p> <p>[7] has 3 bits.</p> <p>The sorted array by bits is [0,1,2,4,8,3,5,6,7]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [1024,512,256,128,64,32,16,8,4,2,1]</p> <p><strong>Output:</strong> [1,2,4,8,16,32,64,128,256,512,1024] <strong>Explantion:</strong> All integers have 1 bit in the binary representation, you should just sort them in ascending order.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 500</code></li> <li><code>0 <= arr[i] <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sortByBits

      public int[] sortByBits(int[] arr)