Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1975 from lynndove/master
Browse files Browse the repository at this point in the history
增加了0056.合并区间的解法二
  • Loading branch information
youngyangyang04 authored Apr 6, 2023
2 parents 277a8e0 + 584654b commit 27ae005
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,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
Expand Down

0 comments on commit 27ae005

Please sign in to comment.