leet-code/move-zeroes/sol.go

18 lines
269 B
Go
Raw Normal View History

2021-11-07 20:12:20 +00:00
package main
// Time: O(n)
// Space: O(1)
func moveZeroes(nums []int) {
zeroes := 0
for idx, num := range nums {
if num == 0 {
zeroes++
} else {
nums[idx-zeroes] = num
}
}
for idx := len(nums) - 1; idx >= len(nums)-zeroes; idx-- {
nums[idx] = 0
}
}