-
Notifications
You must be signed in to change notification settings - Fork 0
/
#283 moveZeroes.py
36 lines (30 loc) · 1.06 KB
/
#283 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
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# Version 3
index = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[index], nums[i] = nums[i], nums[index]
index += 1
# left_ptr = right_ptr = 0
# while left_ptr < len(nums):
# if nums[left_ptr] == 0:
# right_ptr = left_ptr
# while nums[right_ptr] == 0 and right_ptr < len(nums) - 1:
# right_ptr += 1
# nums[left_ptr], nums[right_ptr] = nums[right_ptr], nums[left_ptr]
# left_ptr += 1
# Version 2
# num_zeroes = 0
# length = len(nums)
#
# for i in range(len(nums)):
# if nums[i] == 0:
# num_zeroes += 1
#
# for i in range(num_zeroes):
# nums.remove(0)
# nums.insert(length, 0)