-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_zeroes.py
More file actions
40 lines (30 loc) · 766 Bytes
/
move_zeroes.py
File metadata and controls
40 lines (30 loc) · 766 Bytes
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
class Solution:
def moveZeroes(self, nums):
"""
Do not return anything, modify nums in-place instead.
[0,1,0,3,12]
L
R
[1,0,0,3,12]
L
R
[1,3,12,0,0]
L
R
"""
l = 0
for r in range(len(nums)):
if nums[r] == 0:
continue
nums[l], nums[r] = nums[r], nums[l]
l += 1
return nums
# Time Complexity: O(n)
# Space Complexity: O(1)
if __name__ == "__main__":
solution = Solution()
nums = [0, 1, 0, 3, 12]
result = solution.moveZeroes(nums)
assert result == [1, 3, 12, 0, 0]
print(result)
print("Test Case Passed!")