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 04c1c11

Browse files
solution 992 by go
1 parent 93464bd commit 04c1c11

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎go/992-solution.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* LC#922:Sort Array By Parity II
7+
* Link:https://leetcode-cn.com/problems/sort-array-by-parity-ii/
8+
* Method:DP 双指针
9+
* Remark:比官方的 O(n2) 解法效率还要慢上不少,不知道为什么,初始化 slice 也踩坑,没有初始化 slice len 还不能直接使用下标
10+
*/
11+
func sortArrayByParityII(nums []int) []int {
12+
res := make([]int, len(nums))
13+
j, o := 1, 0
14+
for _, e := range nums {
15+
if e%2 == 0 {
16+
res[o] = e
17+
o += 2
18+
} else {
19+
res[j] = e
20+
j += 2
21+
}
22+
}
23+
return res
24+
}
25+
26+
func main() {
27+
28+
nums := []int{4, 2, 5, 7}
29+
res := sortArrayByParityII(nums)
30+
fmt.Println(res)
31+
32+
}

0 commit comments

Comments
(0)

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