8000 Merge branch 'pull_request/53' into develop · folai/awesome-golang-leetcode@ca57ff6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca57ff6

Browse files
committed
Merge branch 'pull_request/53' into develop
2 parents 028bd3e + 9d38969 commit ca57ff6

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/0283.Move-Zeroes/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# [283. Move Zeroes][title]
2+
3+
## Description
4+
5+
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: [0,1,0,3,12]
11+
Output: [1,3,12,0,0]
12+
```
13+
14+
## NOTES
15+
- You must do this in-place without making a copy of the array.
16+
- Minimize the total number 8000 of operations.
17+
18+
19+
20+
21+
22+
23+
[title]: https://leetcode.com/problems/move-zeroes/
24+

src/0283.Move-Zeroes/Solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Solution
2+
3+
func moveZeroes(nums []int) []int {
4+
count := 0
5+
for _, i := range nums {
6+
if i != 0 {
7+
nums[count] = i
8+
count++
9+
}
10+
}
11+
for i := count; i < len(nums); i++ {
12+
temp := i
13+
nums[temp] = 0
14+
}
15+
return nums
16+
}

src/0283.Move-Zeroes/Solution_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestMoveZeroes(t *testing.T) {
9+
cases := []struct {
10+
name string
11+
inputs []int
12+
expects []int
13+
}{
14+
{"test case 1", []int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}},
15+
}
16+
17+
for _, testcase := range cases {
18+
t.Run(testcase.name, func(t *testing.T) {
19+
result := moveZeroes(testcase.inputs)
20+
if !reflect.DeepEqual(result, testcase.expects) {
21+
t.Fatalf("expected: %v, but got: %v, with input: %v ", testcase.expects, result, testcase.inputs)
22+
}
23+
24+
})
25+
}
26+
}

0 commit comments

Comments
 (0)
0