move-zeroes

This commit is contained in:
Gleb Koval 2021-11-07 20:12:20 +00:00
parent 6a0a9e2b9a
commit 68da3956a9
No known key found for this signature in database
GPG Key ID: DF27F6A77C48FDA0
1 changed files with 17 additions and 0 deletions

17
move-zeroes/sol.go Normal file
View File

@ -0,0 +1,17 @@
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
}
}