赞
踩
题目:
题解:
- func insert(intervals [][]int, newInterval []int) (ans [][]int) {
- left, right := newInterval[0], newInterval[1]
- merged := false
- for _, interval := range intervals {
- if interval[0] > right {
- // 在插入区间的右侧且无交集
- if !merged {
- ans = append(ans, []int{left, right})
- merged = true
- }
- ans = append(ans, interval)
- } else if interval[1] < left {
- // 在插入区间的左侧且无交集
- ans = append(ans, interval)
- } else {
- // 与插入区间有交集,计算它们的并集
- left = min(left, interval[0])
- right = max(right, interval[1])
- }
- }
- if !merged {
- ans = append(ans, []int{left, right})
- }
- return
- }
-
- func min(a, b int) int {
- if a < b {
- return a
- }
- return b
- }
-
- func max(a, b int) int {
- if a > b {
- return a
- }
- return b
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。