Skip to content

Commit 8401b5b

Browse files
committed
283. Move Zeroes.
- Language: Golang. - Time complexity: O(n). - Space complexity: O(1).
1 parent 8932914 commit 8401b5b

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

golang_solutions/283_move_zeroes/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package problem283
22

3+
// Time complexity: O(n). Space complexity: O(1).
34
func moveZeroes(nums []int) {
45
if len(nums) <= 1 {
56
return
@@ -21,3 +22,17 @@ func moveZeroes(nums []int) {
2122
}
2223
}
2324
}
25+
26+
// Time complexity: O(n). Space complexity: O(1).
27+
func moveZeroesV1(nums []int) {
28+
i, j := 0, 1
29+
for j < len(nums) {
30+
if nums[i] == 0 && nums[j] != 0 {
31+
nums[i], nums[j] = nums[j], nums[i]
32+
i += 1
33+
} else if nums[i] != 0 {
34+
i += 1
35+
}
36+
j += 1
37+
}
38+
}

golang_solutions/283_move_zeroes/main_test.go

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,63 @@
11
package problem283
22

33
import (
4-
"fmt"
54
"reflect"
65
"testing"
76
)
87

98
func TestMoveZeroes(t *testing.T) {
9+
testMoveZeroes(t, moveZeroes)
10+
}
11+
12+
func TestMoveZeroesV1(t *testing.T) {
13+
testMoveZeroes(t, moveZeroesV1)
14+
}
15+
16+
func testMoveZeroes(t *testing.T, fn func([]int)) {
1017
testCases := []struct {
11-
nums []int
12-
ans []int
18+
testName string
19+
nums []int
20+
ans []int
1321
}{
14-
{[]int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}},
15-
{[]int{0}, []int{0}},
16-
{[]int{}, []int{}},
17-
{[]int{1, 3, 12, 0, 0}, []int{1, 3, 12, 0, 0}},
18-
{[]int{0, 1, 15, 0, 12, 3, 15}, []int{1, 15, 12, 3, 15, 0, 0}},
22+
// PRESET CASES
23+
{"Preset_case_1", []int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}},
24+
{"Preset_case_2", []int{0}, []int{0}},
25+
// COMMON CASES
26+
{"Trailing_zeros", []int{1, 3, 12, 0, 0}, []int{1, 3, 12, 0, 0}},
27+
{"Leading_zeros", []int{0, 0, 0, 1, 2, 3}, []int{1, 2, 3, 0, 0, 0}},
28+
{"Embedded_zeros", []int{1, 0, 2, 0, 3, 0}, []int{1, 2, 3, 0, 0, 0}},
29+
{
30+
"Alternating_zeros_and_non_zeros_(zero_start)",
31+
[]int{0, 1, 0, 2, 0, 3, 0, 4},
32+
[]int{1, 2, 3, 4, 0, 0, 0, 0},
33+
},
1934
{
20-
[]int{0, 0, 0, -15, 10, -1, 0, 12, 0, 3, 15},
21-
[]int{-15, 10, -1, 12, 3, 15, 0, 0, 0, 0, 0},
35+
"Alternating_zeros and non_zeros (non_zero_start)",
36+
[]int{1, 0, 2, 0, 3, 0, 4},
37+
[]int{1, 2, 3, 4, 0, 0, 0},
2238
},
39+
{
40+
"Leading_and_trailing_zeros",
41+
[]int{0, 0, 0, 1, 3, 12, 0, 0},
42+
[]int{1, 3, 12, 0, 0, 0, 0, 0},
43+
},
44+
{
45+
"Large_numbers",
46+
[]int{1000000, 0, 2147483648, 0, 0, 42},
47+
[]int{1000000, 2147483648, 42, 0, 0, 0},
48+
},
49+
// CORNER CASES
50+
{"No_zeros", []int{1, 6, 1, 24}, []int{1, 6, 1, 24}},
51+
{"All_zeros", []int{0, 0, 0}, []int{0, 0, 0}},
52+
{"Single_Zero", []int{0}, []int{0}},
53+
{"Single_Non_Zero", []int{5}, []int{5}},
2354
}
24-
for _, testCase := range testCases {
25-
testName := fmt.Sprintf("%v", testCase.nums)
26-
t.Run(testName, func(t *testing.T) {
27-
moveZeroes(testCase.nums)
28-
if !reflect.DeepEqual(testCase.nums, testCase.ans) {
29-
t.Errorf("got %v, want %v", testCase.nums, testCase.ans)
55+
56+
for _, tc := range testCases {
57+
t.Run(tc.testName, func(t *testing.T) {
58+
fn(tc.nums)
59+
if !reflect.DeepEqual(tc.nums, tc.ans) {
60+
t.Errorf("got %v, want %v", tc.nums, tc.ans)
3061
}
3162
})
3263
}

0 commit comments

Comments
 (0)