-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathInterval.go
53 lines (47 loc) · 1.13 KB
/
Interval.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
package structures
// Interval 提供区间表示
type Interval struct {
Start int
End int
}
// Interval2Ints 把 Interval 转换成 整型切片
func Interval2Ints(i Interval) []int {
return []int{i.Start, i.End}
}
// IntervalSlice2Intss 把 []Interval 转换成 [][]int
func IntervalSlice2Intss(is []Interval) [][]int {
res := make([][]int, 0, len(is))
for i := range is {
res = append(res, Interval2Ints(is[i]))
}
return res
}
// Intss2IntervalSlice 把 [][]int 转换成 []Interval
func Intss2IntervalSlice(intss [][]int) []Interval {
res := make([]Interval, 0, len(intss))
for _, ints := range intss {
res = append(res, Interval{Start: ints[0], End: ints[1]})
}
return res
}
// QuickSort define
func QuickSort(a []Interval, lo, hi int) {
if lo >= hi {
return
}
p := partitionSort(a, lo, hi)
QuickSort(a, lo, p-1)
QuickSort(a, p+1, hi)
}
func partitionSort(a []Interval, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
if (a[j].Start < pivot.Start) || (a[j].Start == pivot.Start && a[j].End < pivot.End) {
i++
a[j], a[i] = a[i], a[j]
}
}
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}