Skip to content

Commit 693904a

Browse files
committed
Solution of Move Zeros
1 parent 9782217 commit 693904a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

200-300q/283.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
3+
4+
Example:
5+
6+
Input: [0,1,0,3,12]
7+
Output: [1,3,12,0,0]
8+
Note:
9+
10+
You must do this in-place without making a copy of the array.
11+
Minimize the total number of operations.
12+
'''
13+
14+
class Solution(object):
15+
def moveZeroes(self, nums):
16+
"""
17+
:type nums: List[int]
18+
:rtype: void Do not return anything, modify nums in-place instead.
19+
"""
20+
zeroIndex = 0
21+
for index in range(len(nums)):
22+
if nums[index] != 0:
23+
nums[zeroIndex] = nums[index]
24+
zeroIndex += 1
25+
26+
for index in range(zeroIndex, len(nums)):
27+
nums[index] = 0

0 commit comments

Comments
 (0)