博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode56. 合并区间 | Merge Intervals
阅读量:4876 次
发布时间:2019-06-11

本文共 6453 字,大约阅读时间需要 21 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址: 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].

Example 2:

Input: [[1,4],[4,5]]Output: [[1,5]]Explanation: Intervals [1,4] and [4,5] are considerred overlapping.

给出一个区间的集合,请合并所有重叠的区间。

示例 1:

输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入: [[1,4],[4,5]]输出: [[1,5]]解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

84ms
1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14         guard !intervals.isEmpty else {15             return []16         }17 18         var intervals = intervals19         var index = 120         while index < intervals.count {21             let interval = intervals[index]22             var left = 023             var right = index24             while left < right {25                 let leftInterval = intervals[left]26                 if interval.end < leftInterval.start || interval.start > leftInterval.end {27                     left += 128                     continue29                 }30 31                 interval.start = min(leftInterval.start, interval.start)32                 interval.end = max(leftInterval.end, interval.end)33                 intervals.remove(at: left)34                 right -= 135                 index -= 136             }37 38             index += 139         }40         return intervals41     }42 }

100ms

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14         let count = intervals.count15         guard count > 0 else {16             return [Interval]()17         }18         19         var intervals = intervals.sorted { (intervalL, intervalR) -> Bool in20             intervalL.start < intervalR.start21         }22         23         var left = intervals[0].start24         var right = intervals[0].end25         var result = [Interval]()26         for i in 1..

104ms

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14                 if intervals.count < 2 {15             return intervals16         }17         //排序18         let newIner = intervals.sorted { (inter1, inter2) -> Bool in19             return inter1.start < inter2.start20         }21         22         var results = [Interval]()//结果23         results.append(newIner[0])24         for (i,inter) in newIner.enumerated() {25             if i == 0 {26                 continue27             }28             let result = results.last!29             if inter.start <= result.end {
//有重叠30 result.end = max(result.end, inter.end)31 } else {32 results.append(inter)33 }34 }35 return results36 }37 }

112ms

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14         var ivs = intervals15         16         if ivs.count <= 1 {17             return ivs18         }19         20         ivs.sort { $0.start < $1.start }21         22         var res = [Interval]()23 24         for interval in ivs {25             if res.isEmpty || (interval.start > res.last!.end) {26 27                 res.append(interval)28 29             } else {30                 res.last!.end = max(res.last!.end, interval.end)31             }32         }33 34         return res35     }36 }

136ms

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14         guard intervals.count > 1 else{15             return intervals16         }17         var intervals = intervals.sorted { (a:Interval, b:Interval) -> Bool in18             return a.start < b.start19         }20         var res = [Interval]()21         res.append(intervals.first!)22         for i in 1..
= last.end ? now.end : last.end27 last.end = end;28 }else{29 res.append(now)30 }31 }32 33 return res34 }35 }

152ms

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int, _ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func merge(_ intervals: [Interval]) -> [Interval] {14         guard intervals.count > 1 else {15             return intervals16         }17         18         let intervals = intervals.sorted(by: {$0.start < $1.start})19         20         var res: [Interval] = []21         var start = intervals[0].start22         var end = intervals[0].end23         for i in 1..
end {28 res.append(Interval(start, end))29 start = current.start30 end = current.end31 }32 }33 34 end = max(intervals.last!.end, end)35 36 res.append(Interval(start, end))37 38 return res39 }40 }

 

转载于:https://www.cnblogs.com/strengthen/p/9917865.html

你可能感兴趣的文章
input的placeholder的颜色、字号、边距设置
查看>>
B1029 旧键盘 (20 分)
查看>>
理解粒子滤波(particle filter)
查看>>
1-6-04:数组逆序重放
查看>>
PHP之文件目录基础操作方法
查看>>
POJ 2251 Dungeon Master(3D迷宫 bfs)
查看>>
问题 B: 习题6-5 数组元素逆置
查看>>
Xenomai 3 migration
查看>>
windows下apache httpd2.4.26集群完整搭建例子:下载、启动、tomcat集群例子
查看>>
Android应用资源---绘制资源类型(Drawable)(四)
查看>>
bzoj 2155 Xor
查看>>
git 设定全局ignore
查看>>
Rails5 layout 和 template
查看>>
Codeforces Round #460 Div. 2 C.D
查看>>
CodeForces 1110H. Modest Substrings
查看>>
同构伪代码彻底理解using 指令
查看>>
落没的十句经典
查看>>
LIST对象排序问题
查看>>
树总结之并查集趣解
查看>>
Don't repeat yourself
查看>>