283.move-zeroes.go 229 B

12345678910111213141516
  1. func moveZeroes(nums []int) {
  2. n := len(nums)
  3. ptr, cnt := 0, 0
  4. for i := 0; i < n && ptr < n-cnt; i++ {
  5. if nums[i] == 0 {
  6. cnt++
  7. } else {
  8. nums[ptr] = nums[i]
  9. ptr++
  10. }
  11. }
  12. for ptr < n {
  13. nums[ptr] = 0
  14. ptr++
  15. }
  16. }