mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
72 lines
3.4 KiB
HTML
72 lines
3.4 KiB
HTML
|
<p>给你一个整数数组 <code>nums</code> 。</p>
|
|||
|
|
|||
|
<p>开始时,选择一个满足 <code>nums[curr] == 0</code> 的起始位置 <code>curr</code> ,并选择一个移动 <strong>方向</strong> :向左或者向右。</p>
|
|||
|
|
|||
|
<p>此后,你需要重复下面的过程:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>如果 <code>curr</code> 超过范围 <code>[0, n - 1]</code> ,过程结束。</li>
|
|||
|
<li>如果 <code>nums[curr] == 0</code> ,沿当前方向继续移动:如果向右移,则 <strong>递增</strong> <code>curr</code> ;如果向左移,则 <strong>递减</strong> <code>curr</code> 。</li>
|
|||
|
<li>如果 <code>nums[curr] > 0</code>:
|
|||
|
<ul>
|
|||
|
<li>将 <code>nums[curr]</code> 减 1 。</li>
|
|||
|
<li><strong>反转</strong> 移动方向(向左变向右,反之亦然)。</li>
|
|||
|
<li>沿新方向移动一步。</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p>如果在结束整个过程后,<code>nums</code> 中的所有元素都变为 0 ,则认为选出的初始位置和移动方向 <strong>有效</strong> 。</p>
|
|||
|
|
|||
|
<p>返回可能的有效选择方案数目。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><b>示例 1:</b></p>
|
|||
|
|
|||
|
<div class="example-block">
|
|||
|
<p><span class="example-io"><b>输入:</b>nums = [1,0,2,0,3]</span></p>
|
|||
|
|
|||
|
<p><span class="example-io"><b>输出:</b>2</span></p>
|
|||
|
|
|||
|
<p><b>解释:</b></p>
|
|||
|
|
|||
|
<p>可能的有效选择方案如下:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>选择 <code>curr = 3</code> 并向左移动。
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,<strong><u>2</u></strong>,0,3] -> [1,0,1,<strong><u>0</u></strong>,3] -> [1,0,1,0,<strong><u>3</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>1</u></strong>,0,2] -> [1,0,0,<strong><u>0</u></strong>,2] -> [1,0,0,0,<strong><u>2</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>0</u></strong>,0,1] -> [1,<strong><u>0</u></strong>,0,0,1] -> [<strong><u>1</u></strong>,0,0,0,1] -> [0,<strong><u>0</u></strong>,0,0,1] -> [0,0,<strong><u>0</u></strong>,0,1] -> [0,0,0,<strong><u>0</u></strong>,1] -> [0,0,0,0,<strong><u>1</u></strong>] -> [0,0,0,0,0]</code>.</li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li>选择 <code>curr = 3</code> 并向右移动。
|
|||
|
<ul>
|
|||
|
<li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,2,0,<strong><u>3</u></strong>] -> [1,0,2,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>2</u></strong>,0,2] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,1,0,<strong><u>2</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>1</u></strong>,0,1] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,0,0,<strong><u>1</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,0] -> [1,0,<strong><u>0</u></strong>,0,0] -> [1,<strong><u>0</u></strong>,0,0,0] -> [<strong><u>1</u></strong>,0,0,0,0] -> [0,0,0,0,0].</code></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
<p><b>示例 2:</b></p>
|
|||
|
|
|||
|
<div class="example-block">
|
|||
|
<p><span class="example-io"><b>输入:</b>nums = [2,3,4,0,4,1,0]</span></p>
|
|||
|
|
|||
|
<p><span class="example-io"><b>输出:</b>0</span></p>
|
|||
|
|
|||
|
<p><b>解释:</b></p>
|
|||
|
|
|||
|
<p>不存在有效的选择方案。</p>
|
|||
|
</div>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><b>提示:</b></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>1 <= nums.length <= 100</code></li>
|
|||
|
<li><code>0 <= nums[i] <= 100</code></li>
|
|||
|
<li>至少存在一个元素 <code>i</code> 满足 <code>nums[i] == 0</code> 。</li>
|
|||
|
</ul>
|