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 73c0236

Browse files
author
Openset
committed
Add: Sort Array By Parity II
1 parent 58503b4 commit 73c0236

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
package sort_array_by_parity_ii
2+
3+
func sortArrayByParityII(A []int) []int {
4+
i, l := 0, len(A)
5+
for j := 1; j < l; j += 2 {
6+
if A[j]&1 == 0 {
7+
for A[i]&1 == 0 {
8+
i += 2
9+
}
10+
A[i], A[j] = A[j], A[i]
11+
}
12+
}
13+
return A
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
package sort_array_by_parity_ii
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input []int
10+
expected []int
11+
}
12+
13+
func TestSortArrayByParityII(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: []int{4, 2, 5, 7},
17+
expected: []int{4, 5, 2, 7},
18+
},
19+
{
20+
input: []int{2, 3, 1, 1, 4, 0, 0, 4, 3, 3},
21+
expected: []int{2, 3, 0, 1, 4, 1, 0, 3, 4, 3},
22+
},
23+
}
24+
for _, tc := range tests {
25+
output := sortArrayByParityII(tc.input)
26+
if !reflect.DeepEqual(output, tc.expected) {
27+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
28+
}
29+
}
30+
}

0 commit comments

Comments
(0)

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