-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2149.go
More file actions
47 lines (40 loc) · 988 Bytes
/
solution2149.go
File metadata and controls
47 lines (40 loc) · 988 Bytes
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
package solution2149
// ============================================================================
// 2149. Rearrange Array Elements by Sign
// URL: https://leetcode.com/problems/rearrange-array-elements-by-sign/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2149
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_rearrangeArray
Benchmark_rearrangeArray-24 16609581 65.90 ns/op 96 B/op 3 allocs/op
PASS
*/
func rearrangeArray(nums []int) []int {
pos := make([]int, 0, len(nums)/2)
neg := make([]int, 0, len(nums)/2)
for _, n := range nums {
switch {
case n < 0:
neg = append(neg, n)
default:
pos = append(pos, n)
}
}
var a int
var b int
out := make([]int, 0, len(nums))
for i := 0; i < len(nums); i++ {
switch i % 2 {
case 0:
out = append(out, pos[a])
a++
default:
out = append(out, neg[b])
b++
}
}
return out
}