1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-19 02:24:59 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/最少反转次数得到翻转二进制字符串 [minimum-number-of-flips-to-reverse-binary-string].html
2025-12-06 16:04:11 +08:00

44 lines
1.5 KiB
HTML
Raw Permalink 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>给你一个 <strong></strong> 整数 <code>n</code></p>
<p><code>s</code><code>n</code><strong>二进制表示</strong>(不含前导零)。</p>
<p>二进制字符串 <code>s</code><strong>反转</strong> 是指将 <code>s</code> 中的字符按相反顺序排列得到的字符串。</p>
<p>你可以翻转 <code>s</code> 中的任意一位(将 <code>0 → 1</code><code>1 → 0</code>)。每次翻转 <strong></strong> 影响一位。</p>
<p>请返回使 <code>s</code> 等于其原始形式的反转所需的 <strong>最少</strong> 翻转次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 7</span></p>
<p><strong>输出:</strong> <span class="example-io">0</span></p>
<p><strong>解释:</strong></p>
<p>7 的二进制表示为 <code>"111"</code>。其反转也是 <code>"111"</code>,两者相同。因此,不需要翻转。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 10</span></p>
<p><strong>输出:</strong> <span class="example-io">4</span></p>
<p><strong>解释:</strong></p>
<p>10 的二进制表示为 <code>"1010"</code>。其反转为 <code>"0101"</code>。必须翻转所有四位才能使它们相等。因此,所需的最少翻转次数为 4。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>