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

Commit db4185c

Browse files
author
haoc
committed
add: 283
1 parent f4d233a commit db4185c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

‎problems/0283.move-zeroes/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# 0283.move-zeroes
3+
4+
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
5+
6+
请注意 ,必须在不复制数组的情况下原地对数组进行操作。
7+
8+
9+
10+
示例 1:
11+
12+
输入: nums = [0,1,0,3,12]
13+
输出: [1,3,12,0,0]
14+
示例 2:
15+
16+
输入: nums = [0]
17+
输出: [0]
18+
19+
20+
提示:
21+
22+
1 <= nums.length <= 104
23+
-231 <= nums[i] <= 231 - 1
24+
25+
26+
进阶:你能尽量减少完成的操作次数吗?

‎problems/0283.move-zeroes/run.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package zeroes
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Run() {
8+
n := []int{0, 0, 0, 1}
9+
moveZeroes(n)
10+
fmt.Println(n)
11+
}
12+
13+
func moveZeroes(nums []int) {
14+
l := len(nums)
15+
if l == 0 {
16+
return
17+
}
18+
left, right := 0, 0
19+
for i := 0; i < l; i++ {
20+
if nums[right] != 0 {
21+
nums[left], nums[right] = nums[right], nums[left]
22+
left++
23+
}
24+
right++
25+
}
26+
}

‎problems/0283.move-zeroes/run_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package zeroes
3+
4+
import "testing"
5+
6+
func TestRun(t *testing.T) {
7+
Run()
8+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /