LeetCode 283 移动零
有勇气的牛排
24
算法
2025-08-06 22:08:17
题目
给定一个数组 nums
,编写一个函数将所有 0
移动到数组末尾,同时保持非零元素的相对顺序。
请注意,必须在不复制数组的情况下原地对数组进行操作。
示例 1:
输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
示例 2:
输入: nums = [0]
输出: [0]
提示:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
**进阶:**你能尽量减少完成的操作次数吗?
暴力法(不推荐)
- 遍历数组,遇到
0
就删除,并在末尾追加 0
。
- 问题:删除操作0(n),整体复杂度0(n²),效率低。
双指针(推荐)
from typing import List
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
slow = 0
for fast in range(len(nums)):
if nums[fast] != 0:
nums[slow] = nums[fast]
slow += 1
for i in range(slow, len(nums)):
nums[i] = 0
print(nums)
Solution().moveZeroes([0, 1, 0, 3, 12])
Solution().moveZeroes([0])
优化版(双指针 交换法)
from typing import List
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
slow = 0
for fast in range(len(nums)):
print(nums[fast])
if nums[fast] != 0:
nums[fast], nums[slow] = nums[slow], nums[fast]
slow += 1
print(nums)
Solution().moveZeroes([0, 1, 0, 3, 12])
Solution().moveZeroes([0])
<h2><a id="_0"></a>题目</h2>
<p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组末尾,同时保持非零元素的相对顺序。</p>
<p><strong>请注意</strong>,必须在不复制数组的情况下原地对数组进行操作。</p>
<p><strong>示例 1:</strong></p>
<pre><div class="hljs"><code class="lang-shell">输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
</code></div></pre>
<p><strong>示例 2:</strong></p>
<pre><div class="hljs"><code class="lang-shell">输入: nums = [0]
输出: [0]
</code></div></pre>
<p><strong>提示</strong>:</p>
<ul>
<li><code>1 <= nums.length <= 104</code></li>
<li><code>-231 <= nums[i] <= 231 - 1</code></li>
</ul>
<p>**进阶:**你能尽量减少完成的操作次数吗?</p>
<h2><a id="_29"></a>暴力法(不推荐)</h2>
<ul>
<li>遍历数组,遇到 <code>0</code> 就删除,并在末尾追加 <code>0</code>。</li>
<li>问题:删除操作0(n),整体复杂度0(n²),效率低。</li>
</ul>
<h2><a id="_34"></a>双指针(推荐)</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">moveZeroes</span>(<span class="hljs-params">self, nums: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>]</span>) -> <span class="hljs-literal">None</span>:
slow = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> fast <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(nums)):
<span class="hljs-keyword">if</span> nums[fast] != <span class="hljs-number">0</span>:
nums[slow] = nums[fast]
slow += <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(slow, <span class="hljs-built_in">len</span>(nums)):
nums[i] = <span class="hljs-number">0</span>
<span class="hljs-built_in">print</span>(nums)
Solution().moveZeroes([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">12</span>])
Solution().moveZeroes([<span class="hljs-number">0</span>])
</code></div></pre>
<h2><a id="__58"></a>优化版(双指针 交换法)</h2>
<ul>
<li>
<p>如果 <code>fast</code>指向非零,并且 <code>fast != slow</code>,就交换 <code>nums[slow]</code>,和 <code>nums[fast]</code>。</p>
</li>
<li>
<p>这样就不用最后在补零。</p>
</li>
</ul>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">moveZeroes</span>(<span class="hljs-params">self, nums: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>]</span>) -> <span class="hljs-literal">None</span>:
slow = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> fast <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(nums)):
<span class="hljs-built_in">print</span>(nums[fast])
<span class="hljs-keyword">if</span> nums[fast] != <span class="hljs-number">0</span>:
nums[fast], nums[slow] = nums[slow], nums[fast]
slow += <span class="hljs-number">1</span>
<span class="hljs-built_in">print</span>(nums)
Solution().moveZeroes([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">12</span>])
Solution().moveZeroes([<span class="hljs-number">0</span>])
</code></div></pre>
评论区