mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 0056.合并区间.md
This commit is contained in:
@ -171,7 +171,32 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func merge(intervals [][]int) [][]int {
|
||||||
|
sort.Slice(intervals, func(i, j int) bool {
|
||||||
|
return intervals[i][0]<intervals[j][0]
|
||||||
|
})
|
||||||
|
|
||||||
|
res:=[][]int{}
|
||||||
|
prev:=intervals[0]
|
||||||
|
|
||||||
|
for i:=1;i<len(intervals);i++{
|
||||||
|
cur :=intervals[i]
|
||||||
|
if prev[1]<cur[0]{
|
||||||
|
res=append(res,prev)
|
||||||
|
prev=cur
|
||||||
|
}else {
|
||||||
|
prev[1]=max(prev[1],cur[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res=append(res,prev)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func max(a, b int) int {
|
||||||
|
if a > b { return a }
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -179,4 +204,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user