Skip to content

Commit

Permalink
增加了0056.合并区间的解法二
Browse files Browse the repository at this point in the history
  • Loading branch information
lynndove committed Mar 23, 2023
1 parent 5d33abb commit 584654b
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 @@ -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
Expand Down

0 comments on commit 584654b

Please sign in to comment.