-
Notifications
You must be signed in to change notification settings - Fork 17
/
int.go
90 lines (81 loc) · 1.26 KB
/
int.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package helpers
import (
"sort"
)
// Merge2ArraysInt64 -
//nolint
func Merge2ArraysInt64(a, b []int64) []int64 {
if len(a) == 0 && len(b) == 0 {
return []int64{}
}
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
result := make([]int64, 0)
maxLen := len(a)
if len(b) > len(a) {
maxLen = len(b)
a, b = b, a
}
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
for j, i := 0, 0; i < maxLen; {
if j >= len(b) {
result = append(result, a[i])
i++
continue
}
if i >= len(a) {
result = append(result, b[j])
j++
continue
}
if a[i] < b[j] {
result = append(result, a[i])
i++
} else if a[i] > b[j] {
result = append(result, b[j])
j++
} else {
result = append(result, a[i])
i++
j++
}
}
return result
}
// MaxInt -
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// MaxInt64 -
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
// MinInt -
func MinInt(a, b int) int {
if a > b {
return b
}
return a
}
// IsInt64PointersEqual -
func IsInt64PointersEqual(a, b *int64) bool {
switch {
case a == nil && b == nil:
return true
case a != nil && b != nil:
return *a == *b
default:
return false
}
}