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)/将 1 移动到末尾的最大操作次数 [maximum-number-of-operations-to-move-ones-to-the-end].html
2024-08-06 08:46:50 +08:00

49 lines
1.9 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>给你一个 <span data-keyword="binary-string">二进制字符串</span> <code>s</code></p>
<p>你可以对这个字符串执行 <strong>任意次</strong> 下述操作:</p>
<ul>
<li>选择字符串中的任一下标 <code>i</code> <code>i + 1 &lt; s.length</code> ),该下标满足 <code>s[i] == '1'</code><code>s[i + 1] == '0'</code></li>
<li>将字符 <code>s[i]</code><strong>右移 </strong>直到它到达字符串的末端或另一个 <code>'1'</code>。例如,对于 <code>s = "010010"</code>,如果我们选择 <code>i = 1</code>,结果字符串将会是 <code>s = "0<strong><u>001</u></strong>10"</code></li>
</ul>
<p>返回你能执行的<strong> 最大 </strong>操作次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "1001101"</span></p>
<p><strong>输出:</strong> <span class="example-io">4</span></p>
<p><strong>解释:</strong></p>
<p>可以执行以下操作:</p>
<ul>
<li>选择下标 <code>i = 0</code>。结果字符串为 <code>s = "<u><strong>001</strong></u>1101"</code></li>
<li>选择下标 <code>i = 4</code>。结果字符串为 <code>s = "0011<u><strong>01</strong></u>1"</code></li>
<li>选择下标 <code>i = 3</code>。结果字符串为 <code>s = "001<strong><u>01</u></strong>11"</code></li>
<li>选择下标 <code>i = 2</code>。结果字符串为 <code>s = "00<strong><u>01</u></strong>111"</code></li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "00111"</span></p>
<p><strong>输出:</strong> <span class="example-io">0</span></p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s[i]</code><code>'0'</code><code>'1'</code></li>
</ul>