class Solution:
def moveZeroes(self, nums: List[int]) -> None:
idxInsert = 0
for i in range(len(nums)):
if nums[i] != 0:
# To avoid the duplicate assign
if i != idxInsert:
nums[idxInsert] = nums[i]
idxInsert += 1
else:
idxInsert += 1
for i in range(idxInsert, len(nums)):
nums[i] = 0
return nums
Time Complexity: O(N)
Space Complexity: O(1)
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
snowBallSize = 0 # Consecutive zero
for i in range(len(nums)):
if nums[i] == 0:
snowBallSize += 1
elif nums[i] != 0 and snowBallSize > 0:
nums[i - snowBallSize] = nums[i]
nums[i] = 0
return nums
Time Complexity: O(N)
Space Complexity: O(1)
Time Complexity: O(N)
Space Complexity: O(1)