-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzeroCollect2.py
More file actions
41 lines (37 loc) · 1.24 KB
/
zeroCollect2.py
File metadata and controls
41 lines (37 loc) · 1.24 KB
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
# Solution with Maintaining relative order
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
# Without maintaining relative order
"""
temp = 0
for i in range(len(nums)-1,-1,-1):
if nums[i] == 0:
nums[i] = nums[temp]
nums[temp] = 0
temp = temp - 1
else:
if temp == 0:
temp = i
"""
# Maintaining relative order
for i in range(len(nums)):
if nums[i] == 0:
for j in range(i, len(nums)):
if nums[j] != 0:
nums[i] = nums[j]
nums[j] = 0
break
#print nums
# Easiest thinking - just place all the non zero to front and add 0s
lastNonZero = -1
for i in range(len(nums)):
if nums[i] != 0:
lastNonZero += 1
nums[lastNonZero] = nums[i]
for i in range(lastNonZero+1,len(nums)):
nums[i] = 0
#nums = sorted(nums)