mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-12-17 17:52:34 +08:00
55 lines
2.2 KiB
HTML
55 lines
2.2 KiB
HTML
<p>You are given an integer array <code>nums</code>.</p>
|
|
|
|
<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>
|
|
|
|
<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>
|
|
|
|
<p>Return the resulting sorted array.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,4]</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">[4,4,5]</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>Binary reflections are:</p>
|
|
|
|
<ul>
|
|
<li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li>
|
|
<li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li>
|
|
<li>4 -> (binary) <code>100</code> -> (reversed) <code>001</code> -> 1</li>
|
|
</ul>
|
|
Sorting by the reflected values gives <code>[4, 4, 5]</code>.</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">nums = [3,6,5,8]</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">[8,3,6,5]</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>Binary reflections are:</p>
|
|
|
|
<ul>
|
|
<li>3 -> (binary) <code>11</code> -> (reversed) <code>11</code> -> 3</li>
|
|
<li>6 -> (binary) <code>110</code> -> (reversed) <code>011</code> -> 3</li>
|
|
<li>5 -> (binary) <code>101</code> -> (reversed) <code>101</code> -> 5</li>
|
|
<li>8 -> (binary) <code>1000</code> -> (reversed) <code>0001</code> -> 1</li>
|
|
</ul>
|
|
Sorting by the reflected values gives <code>[8, 3, 6, 5]</code>.<br />
|
|
Note that 3 and 6 have the same reflection, so we arrange them in increasing order of original value.</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 100</code></li>
|
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
|
</ul>
|