Skip to content

Commit 5d16014

Browse files
committed
feat: commit solution 283
1 parent 119dccc commit 5d16014

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

_sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [169.多数元素](solution/100-199/0169.majority-element/)
2626
- [217.存在重复元素](solution/200-299/0217.contains-duplicate/)
2727
- [219.存在重复元素 II](solution/200-299/0219.contains-duplicate-ii/)
28+
- [283.移动零](solution/200-299/0283.move-zeroes/)
2829

2930

3031

index-tags.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
3131
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
3232
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
33+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
3334
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
3435
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
35-
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
36+
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
3637

3738
#### **哈希表**
3839

@@ -59,7 +60,7 @@
5960
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
6061
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
6162
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
62-
63+
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
6364

6465
#### **链表**
6566

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
| [270](https://leetcode-cn.com/problems/closest-binary-search-tree-value) | [最接近的二叉搜索树值](/solution/200-299/0270.closest-binary-search-tree-value/) | ``,`二分查找` | <font color=green>简单</font> |
9292
| [276](https://leetcode-cn.com/problems/paint-fence) | [栅栏涂色](/solution/200-299/0276.paint-fence/) | `动态规划` | <font color=green>简单</font> |
9393
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> |
94-
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> |
94+
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
9595
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> |
9696
| [292](https://leetcode-cn.com/problems/nim-game) | [nim 游戏](/solution/200-299/0292.nim-game/) | `脑筋急转弯`,`极小化极大` | <font color=green>简单</font> |
9797
| [293](https://leetcode-cn.com/problems/flip-game) | [翻转游戏](/solution/200-299/0293.flip-game/) | `字符串` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [283.移动零](https://leetcode-cn.com/problems/move-zeroes/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组的末尾,同时保持非零元素的相对顺序。</p>
7+
8+
<p><strong>示例:</strong></p>
9+
10+
<pre><strong>输入:</strong> <code>[0,1,0,3,12]</code>
11+
<strong>输出:</strong> <code>[1,3,12,0,0]</code></pre>
12+
13+
<p><strong>说明</strong>:</p>
14+
15+
<ol>
16+
<li>必须在原数组上操作,不能拷贝额外的数组。</li>
17+
<li>尽量减少操作次数。</li>
18+
</ol>
19+
</div>
20+
21+
22+
### 解题思路
23+
24+
![](http://lc-photo.xwlin.com/283.gif)
25+
26+
### 代码实现
27+
28+
<!-- tabs:start -->
29+
30+
#### **Golang**
31+
```go
32+
func moveZeroes(nums []int) {
33+
p := 0
34+
for i := 0; i < len(nums); i++ {
35+
if nums[i] != 0 {
36+
nums[p] = nums[i]
37+
if p != i {
38+
nums[i] = 0
39+
}
40+
p++
41+
}
42+
}
43+
}
44+
```
45+
46+
47+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=283 lang=golang
5+
*
6+
* [283] 移动零
7+
*/
8+
9+
// @lc code=start
10+
func moveZeroes(nums []int) {
11+
p := 0
12+
for i := 0; i < len(nums); i++ {
13+
if nums[i] != 0 {
14+
nums[p] = nums[i]
15+
if p != i {
16+
nums[i] = 0
17+
}
18+
p++
19+
}
20+
}
21+
}
22+
23+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMoveZeroes(t *testing.T) {
8+
var nums []int
9+
var ret []int
10+
11+
nums = []int{0, 1, 0, 3, 12}
12+
ret = []int{1, 3, 12, 0, 0}
13+
moveZeroes(nums)
14+
for k, v := range nums {
15+
if v != ret[k] {
16+
t.Fatalf("case fails: %v\n", ret)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)