-
Notifications
You must be signed in to change notification settings - Fork 2
/
MoveZeroes.py
42 lines (33 loc) · 1.06 KB
/
MoveZeroes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import List
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
p = 0
for i in range(0, len(nums)):
if nums[i] != 0:
nums[p], nums[i] = nums[i], nums[p]
p += 1
# Print Result
for n in nums:
print(n, end = " ")
print()
def moveZeroes_2(self, nums: List[int]) -> None:
lastNoneZeroPosition = 0
# Move all the none zero elements to the front of the array
for i in range(len(nums)):
if nums[i] != 0:
nums[lastNoneZeroPosition] = nums[i]
lastNoneZeroPosition += 1
# Fill the rest elements of the array with zeros
for i in range(lastNoneZeroPosition, len(nums)):
nums[i] = 0
# Print Result
for n in nums:
print(n, end = " ")
print()
Solution().moveZeroes_2([0,1,0,3,12])
# expected: 1 3 12 0 0
Solution().moveZeroes([2,1])
# expected: 2 1