Leetcode - Move Zeroes
https://leetcode.com/problems/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]
Just like this
func moveZeroes(nums []int) { i := 0 j := i + 1 for { for i < len(nums) && nums[i] != 0 { i++ } j = i + 1 for j < len(nums) && nums[j] == 0 { j++ } if j >= len(nums) { break } nums[i], nums[j] = nums[j], nums[i] i++ } }