-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path283_move_zeros.go
More file actions
37 lines (35 loc) · 927 Bytes
/
283_move_zeros.go
File metadata and controls
37 lines (35 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package leetcode
// https://leetcode.com/problems/move-zeroes/description/
//
// Naive implementation:
// Start from beginneing, if we meet a zero, iterate from there to find the first non zero and swap
func moveZeroesNaive(nums []int) {
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
for j := i + 1; j < len(nums); j++ {
if nums[j] != 0 {
tmp := nums[j]
nums[j] = nums[i]
nums[i] = tmp
break
}
}
}
}
}
// Cleaner way:
// We can lessen a loop by remember where the next non zero should be placed
// Then only need 1 loop to iterate swap non zero into their place
// If the current position is a non zero, it will simply be swap into a latter position
// which maintains original ordering
func moveZeroesClean(nums []int) {
nonZero := 0
for i := 0; i < len(nums); i++ {
if nums[i] != 0 {
tmp := nums[nonZero]
nums[nonZero] = nums[i]
nums[i] = tmp
nonZero++
}
}
}