mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-10-26 07:18:56 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			33 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <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>
 | |
| 
 | |
| <p>Return <em>the array after sorting it</em>.</p>
 | |
| 
 | |
| <p> </p>
 | |
| <p><strong>Example 1:</strong></p>
 | |
| 
 | |
| <pre>
 | |
| <strong>Input:</strong> arr = [0,1,2,3,4,5,6,7,8]
 | |
| <strong>Output:</strong> [0,1,2,4,8,3,5,6,7]
 | |
| <strong>Explantion:</strong> [0] is the only integer with 0 bits.
 | |
| [1,2,4,8] all have 1 bit.
 | |
| [3,5,6] have 2 bits.
 | |
| [7] has 3 bits.
 | |
| The sorted array by bits is [0,1,2,4,8,3,5,6,7]
 | |
| </pre>
 | |
| 
 | |
| <p><strong>Example 2:</strong></p>
 | |
| 
 | |
| <pre>
 | |
| <strong>Input:</strong> arr = [1024,512,256,128,64,32,16,8,4,2,1]
 | |
| <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.
 | |
| </pre>
 | |
| 
 | |
| <p> </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>
 |