1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-20 19:03:47 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/将数字变成二进制回文数的最少操作(English) [minimum-operations-to-make-binary-palindrome].html
2025-12-17 09:38:38 +08:00

132 lines
4.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<p>You are given an integer array <code>nums</code>.</p>
<p>For each element <code>nums[i]</code>, you may perform the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Increase <code>nums[i]</code> by 1, or</li>
<li>Decrease <code>nums[i]</code> by 1.</li>
</ul>
<p>A number is called a <strong>binary palindrome</strong> if its binary representation without leading zeros reads the same forward and backward.</p>
<p>Your task is to return an integer array <code>ans</code>, where <code>ans[i]</code> represents the <strong>minimum</strong> number of operations required to convert <code>nums[i]</code> into a <strong>binary palindrome</strong>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[0,1,1]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>nums[i]</code></th>
<th style="border: 1px solid black;">Binary(<code>nums[i]</code>)</th>
<th style="border: 1px solid black;">Nearest<br />
Palindrome</th>
<th style="border: 1px solid black;">Binary<br />
(Palindrome)</th>
<th style="border: 1px solid black;">Operations Required</th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">Already palindrome</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">10</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">11</td>
<td style="border: 1px solid black;">Increase by 1</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;">100</td>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;">11</td>
<td style="border: 1px solid black;">Decrease by 1</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [0, 1, 1]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [6,7,12]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,0,3]</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal set of operations:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>nums[i]</code></th>
<th style="border: 1px solid black;">Binary(<code>nums[i]</code>)</th>
<th style="border: 1px solid black;">Nearest<br />
Palindrome</th>
<th style="border: 1px solid black;">Binary<br />
(Palindrome)</th>
<th style="border: 1px solid black;">Operations Required</th>
<th style="border: 1px solid black;"><code>ans[i]</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">6</td>
<td style="border: 1px solid black;">110</td>
<td style="border: 1px solid black;">5</td>
<td style="border: 1px solid black;">101</td>
<td style="border: 1px solid black;">Decrease by 1</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">7</td>
<td style="border: 1px solid black;">111</td>
<td style="border: 1px solid black;">7</td>
<td style="border: 1px solid black;">111</td>
<td style="border: 1px solid black;">Already palindrome</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">12</td>
<td style="border: 1px solid black;">1100</td>
<td style="border: 1px solid black;">15</td>
<td style="border: 1px solid black;">1111</td>
<td style="border: 1px solid black;">Increase by 3</td>
<td style="border: 1px solid black;">3</td>
</tr>
</tbody>
</table>
<p>Thus, <code>ans = [1, 0, 3]</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
<li><code><sup></sup>1 &lt;= nums[i] &lt;=<sup> </sup>5000</code></li>
</ul>