[
two pointers
array
]
Leetcode 0283 Move Zeros
Problem statement
https://leetcode.com/problems/move-zeroes/
Solution
Use two pointers, one is for current place to write number and another goes one by one through all elements.
Complexity
Time complexity is O(n), space complexity is O(1).
Code
class Solution:
def moveZeroes(self, nums):
beg, end = 0, 0
while end < len(nums):
if nums[end] != 0:
nums[beg], nums[end] = nums[end], nums[beg]
beg += 1
end += 1