Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def moveZeroes(self, nums: List[int]) -> None:
- """
- Do not return anything, modify nums in-place instead.
- """
- # slow pointer to keep tracker of the non-target elements
- left = 0
- # fast pointer to find the non-
- for right in range(len(nums)):
- if nums[right]:
- nums[left], nums[right] = nums[right], nums[left]
- left += 1
- return nums
- # However note this does not work because you are literally re-assigning the array
- # because you are reassigning the array rather than modifying in-place
- class Solution:
- def moveZeroes(self, nums: List[int]) -> None:
- """
- Do not return anything, modify nums in-place instead.
- """
- slow = 0
- for fast in range(len(nums)):
- if nums[fast] != 0:
- nums[slow] = nums[fast]
- slow += 1
- nums = nums[:slow] + (len(nums) - slow)*[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement