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 7348be3

Browse files
solution 905 by solution
1 parent 0f653b8 commit 7348be3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎go/905-solution.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* LC#905. Sort Array By Parity
7+
* Link:https://leetcode-cn.com/problems/sort-array-by-parity/
8+
* 思路1:1次遍历输出,维护头尾指针s,m 分别根据奇数偶数类型选择从头尾插入
9+
*/
10+
func sortArrayByParity(nums []int) []int {
11+
i, j := 0, len(nums)-1
12+
for i < j {
13+
fmt.Println("i:", i, "j:", j)
14+
if nums[i]%2 > nums[j]%2 {
15+
nums[i], nums[j] = nums[j], nums[i]
16+
}
17+
if nums[i]%2 == 0 {
18+
i++
19+
}
20+
if nums[j]%2 == 1 {
21+
j--
22+
}
23+
}
24+
return nums
25+
}
26+
27+
func main() {
28+
input := []int{3, 1, 2, 4}
29+
res := sortArrayByParity(input)
30+
fmt.Println(res)
31+
}

0 commit comments

Comments
(0)

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