[LeetCode-Eazy] Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
func moveZeroes(nums []int)  {
	for i, j := 0, 0; i < len(nums); i++ {
		if nums[i] != 0 {
			nums[i], nums[j] = nums[j], nums[i]
			j++
		}
	}
}
i 不管怎樣都會 ++

當 i 遇到 0 會獨留 j 在 0 的位置上

所以才要在當 i != 0 的時候交換

這樣才可以把 0 往後挪

!= 0 的弄到前面去