Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit99efce0

Browse files
authored
Merge pull request6boris#56 from kylesliu/develop
Develop
2 parents7c69869 +ff8fdf4 commit99efce0

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 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+
funcmoveZeroes(nums []int) []int {
4+
count:=0
5+
for_,i:=rangenums {
6+
ifi!=0 {
7+
nums[count]=i
8+
count++
9+
}
10+
}
11+
fori:=count;i<len(nums);i++ {
12+
temp:=i
13+
nums[temp]=0
14+
}
15+
returnnums
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+
funcTestMoveZeroes(t*testing.T) {
9+
cases:= []struct {
10+
namestring
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:=rangecases {
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)

[8]ページ先頭

©2009-2025 Movatter.jp