-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.go
48 lines (45 loc) · 784 Bytes
/
21.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package algo
func reorderOddEven(nums []int) []int {
if len(nums) == 0 {
return nil
}
start := 0
end := len(nums) - 1
for start < end {
for start < end && nums[start]&0x1 != 0 {
start++
}
for start < end && nums[end]&0x1 == 0 {
end--
}
if start < end {
nums[start], nums[end] = nums[end], nums[start]
}
}
return nums
}
func reorderOddEvenTwo(nums []int, f func(int) bool) []int {
if len(nums) == 0 {
return nil
}
start := 0
end := len(nums) - 1
for start < end {
for start < end && isEven(nums[start]) {
start++
}
for start < end && !isEven(nums[end]) {
end--
}
if start < end {
nums[start], nums[end] = nums[end], nums[start]
}
}
return nums
}
func isEven(i int) bool {
if i&0x1 != 0 {
return true
}
return false
}