1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/使两个整数相等的位更改次数 [number-of-bit-changes-to-make-two-integers-equal].html
2024-08-06 08:46:50 +08:00

51 lines
1.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个正整数 <code>n</code><code>k</code></p>
<p>你可以选择 <code>n</code><strong>二进制表示</strong> 中任意一个值为 1 的位,并将其改为 0。</p>
<p>返回使得 <code>n</code> 等于 <code>k</code> 所需要的更改次数。如果无法实现,返回 -1。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 13, k = 4</span></p>
<p><strong>输出:</strong> <span class="example-io">2</span></p>
<p><strong>解释:</strong><br />
最初,<code>n</code><code>k</code> 的二进制表示分别为 <code>n = (1101)<sub>2</sub></code><code>k = (0100)<sub>2</sub></code></p>
<p>我们可以改变 <code>n</code> 的第一位和第四位。结果整数为 <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code></p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 21, k = 21</span></p>
<p><strong>输出:</strong> <span class="example-io">0</span></p>
<p><strong>解释:</strong><br />
<code>n</code><code>k</code> 已经相等,因此不需要更改。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 14, k = 13</span></p>
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
<p><strong>解释:</strong><br />
无法使 <code>n</code> 等于 <code>k</code></p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li>
</ul>