mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
增加了0056.合并区间的解法二
This commit is contained in:
@ -174,6 +174,34 @@ func max(a, b int) int {
|
||||
return b
|
||||
}
|
||||
```
|
||||
```go
|
||||
// 版本2
|
||||
func merge(intervals [][]int) [][]int {
|
||||
if len(intervals) == 1 {
|
||||
return intervals
|
||||
}
|
||||
sort.Slice(intervals, func(i, j int) bool {
|
||||
return intervals[i][0] < intervals[j][0]
|
||||
})
|
||||
res := make([][]int, 0)
|
||||
res = append(res, intervals[0])
|
||||
for i := 1; i < len(intervals); i++ {
|
||||
if intervals[i][0] <= res[len(res)-1][1]{
|
||||
res[len(res)-1][1] = max56(res[len(res)-1][1],intervals[i][1])
|
||||
} else {
|
||||
res = append(res, intervals[i])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
func max56(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Javascript
|
||||
```javascript
|
||||
|
Reference in New Issue
Block a user