-
Notifications
You must be signed in to change notification settings - Fork 2
/
MoveZeroes.py
39 lines (34 loc) · 1.01 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
'''
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.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
'''
class Solution(object):
def moveZeroes(self, nums):
l = len(nums)
if l<2:
return
i,j = 0,1
while j<l:
if not nums[i] and nums[j]:
nums[i] = nums[j]
nums[j] = 0
i+=1
j+=1
elif not nums[i] and not nums[j]:
j+=1
else:
i+=1
j+=1
s = Solution()
example0 = []
example1 = [1,0,1,0,2,4]
example2 = [0,6,0,4,5,4,5,6,0]
s.moveZeroes(example0)
s.moveZeroes(example1)
s.moveZeroes(example2)
assert [] == example0
assert [1,1,2,4,0,0] == example1
assert [6,4,5,4,5,6,0,0,0] == example2