Move Zeroes - Solution
Solutions and explanations
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
left = 0
for right in range(len(nums)):
if nums[right] != 0:
nums[left], nums[right] = nums[right], nums[left]
left += 1
Complexity Analysis
Here, n is the input size.
- Time Complexity:
O(n)– Each element is traversed once by the pointers. - Space Complexity:
O(1)– The modification is performed in-place using a constant amount of extra space, regardless of the input size.