mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
添加 problem 56
This commit is contained in:
70
Algorithms/56. Merge Intervals/56. Merge Intervals.go
Normal file
70
Algorithms/56. Merge Intervals/56. Merge Intervals.go
Normal file
@ -0,0 +1,70 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for an interval.
|
||||
* type Interval struct {
|
||||
* Start int
|
||||
* End int
|
||||
* }
|
||||
*/
|
||||
|
||||
type Interval struct {
|
||||
Start int
|
||||
End int
|
||||
}
|
||||
|
||||
func merge_(intervals []Interval) []Interval {
|
||||
if len(intervals) == 0 {
|
||||
return intervals
|
||||
}
|
||||
quickSort(intervals, 0, len(intervals)-1)
|
||||
res := make([]Interval, 0)
|
||||
res = append(res, intervals[0])
|
||||
curIndex := 0
|
||||
for i := 1; i < len(intervals); i++ {
|
||||
if intervals[i].Start > res[curIndex].End {
|
||||
curIndex++
|
||||
res = append(res, intervals[i])
|
||||
} else {
|
||||
res[curIndex].End = max(intervals[i].End, res[curIndex].End)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func min(a int, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
} else {
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
72
Algorithms/56. Merge Intervals/56. Merge Intervals_test.go
Normal file
72
Algorithms/56. Merge Intervals/56. Merge Intervals_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question56 struct {
|
||||
para56
|
||||
ans56
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para56 struct {
|
||||
one []Interval
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans56 struct {
|
||||
one []Interval
|
||||
}
|
||||
|
||||
func Test_Problem56(t *testing.T) {
|
||||
|
||||
qs := []question56{
|
||||
|
||||
question56{
|
||||
para56{[]Interval{}},
|
||||
ans56{[]Interval{}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 2, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 1, End: 4}, Interval{Start: 4, End: 5}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 5}}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 3, End: 6}, Interval{Start: 5, End: 10}, Interval{Start: 9, End: 18}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 18}}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 15, End: 18}, Interval{Start: 8, End: 10}, Interval{Start: 2, End: 6}, Interval{Start: 1, End: 3}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 2, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
|
||||
},
|
||||
|
||||
question56{
|
||||
para56{[]Interval{Interval{Start: 1, End: 4}, Interval{Start: 1, End: 5}}},
|
||||
ans56{[]Interval{Interval{Start: 1, End: 5}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 56------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans56, q.para56
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, merge_(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
27
Algorithms/56. Merge Intervals/README.md
Normal file
27
Algorithms/56. Merge Intervals/README.md
Normal file
@ -0,0 +1,27 @@
|
||||
# [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a collection of intervals, merge all overlapping intervals.
|
||||
|
||||
Example 1:
|
||||
|
||||
```
|
||||
Input: [[1,3],[2,6],[8,10],[15,18]]
|
||||
Output: [[1,6],[8,10],[15,18]]
|
||||
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```
|
||||
Input: [[1,4],[4,5]]
|
||||
Output: [[1,5]]
|
||||
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
合并给的多个区间,区间有重叠的要进行区间合并。
|
||||
|
||||
思路,先按照区间起点进行排序。然后从区间起点小的开始扫描,依次合并每个有重叠的区间。
|
Reference in New Issue
Block a user