|
| 1 | +<h2><a href="https://leetcode.com/problems/sort-integers-by-binary-reflection">4150. Sort Integers by Binary Reflection</a></h2><h3>Easy</h3><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>The <strong>binary reflection</strong> of a <strong>positive</strong> integer is defined as the number obtained by reversing the order of its <strong>binary</strong> digits (ignoring any leading zeros) and interpreting the resulting binary number as a decimal.</p> |
| 4 | + |
| 5 | +<p>Sort the array in <strong>ascending</strong> order based on the binary reflection of each element. If two different numbers have the same binary reflection, the <strong>smaller</strong> original number should appear first.</p> |
| 6 | + |
| 7 | +<p>Return the resulting sorted array.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,5,4]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">[4,4,5]</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<p>Binary reflections are:</p> |
| 20 | + |
| 21 | +<ul> |
| 22 | + <li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li> |
| 23 | + <li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li> |
| 24 | + <li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li> |
| 25 | +</ul> |
| 26 | +Sorting by the reflected values gives <code>[4, 4, 5]</code>.</div> |
| 27 | + |
| 28 | +<p><strong class="example">Example 2:</strong></p> |
| 29 | + |
| 30 | +<div class="example-block"> |
| 31 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,6,5,8]</span></p> |
| 32 | + |
| 33 | +<p><strong>Output:</strong> <span class="example-io">[8,3,6,5]</span></p> |
| 34 | + |
| 35 | +<p><strong>Explanation:</strong></p> |
| 36 | + |
| 37 | +<p>Binary reflections are:</p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li>3 -> (binary) <code>11</code> -> (reversed) <code>11</code> -> 3</li> |
| 41 | + <li>6 -> (binary) <code>110</code> -> (reversed) <code>011</code> -> 3</li> |
| 42 | + <li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li> |
| 43 | + <li>8 -> (binary) <code>1000</code> -> (reversed) <code>0001</code> -> 1</li> |
| 44 | +</ul> |
| 45 | +Sorting by the reflected values gives <code>[8, 3, 6, 5]</code>.<br /> |
| 46 | +Note that 3 and 6 have the same reflection, so we arrange them in increasing order of original value.</div> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>1 <= nums.length <= 100</code></li> |
| 53 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 54 | +</ul> |
0 commit comments