Skip to content

Commit fa8696e

Browse files
committed
Sync LeetCode submission - Move Zeroes - Runtime - 4 ms (59.34%), Memory - 20.5 MB (6.40%)
1 parent c1c7262 commit fa8696e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

0283-move-zeroes/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p>Given an integer array <code>nums</code>, move all <code>0</code>&#39;s to the end of it while maintaining the relative order of the non-zero elements.</p>
2+
3+
<p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<pre><strong>Input:</strong> nums = [0,1,0,3,12]
8+
<strong>Output:</strong> [1,3,12,0,0]
9+
</pre><p><strong class="example">Example 2:</strong></p>
10+
<pre><strong>Input:</strong> nums = [0]
11+
<strong>Output:</strong> [0]
12+
</pre>
13+
<p>&nbsp;</p>
14+
<p><strong>Constraints:</strong></p>
15+
16+
<ul>
17+
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
18+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
19+
</ul>
20+
21+
<p>&nbsp;</p>
22+
<strong>Follow up:</strong> Could you minimize the total number of operations done?

0283-move-zeroes/solution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def moveZeroes(self, nums: List[int]) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
j = 0
7+
for i in range(len(nums)):
8+
if nums[i] != 0:
9+
nums[i], nums[j] = nums[j], nums[i]
10+
j += 1
11+
return nums
12+

0 commit comments

Comments
 (0)