为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode57. 插入区间 | Insert Interval

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

给出一个无重叠的 ,按照区间起始端点排序的区间列表。

在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

示例 1:

输入: intervals = [[1,3],[6,9]], newInterval = [2,5]
输出: [[1,5],[6,9]]

示例 2:

输入: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
输出: [[1,2],[3,10],[12,16]]
解释: 这是因为新的区间 [4,8][3,5],[6,7],[8,10] 重叠。

40ms
 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     
14     func merge(_ intervals: [Interval]) -> [Interval] {
15          
16         let intervalsSorted =  intervals.sorted(by: { $0.start < $1.start } )
17       
18         var theIntervals = [Interval]()
19         
20          
21         for interval in intervalsSorted  { 
22             
23             if (theIntervals.count == 0 || theIntervals.last!.end <  interval.start) { 
24                 theIntervals.append(interval) 
25             }
26             // merge
27             else {
28                 
29                  theIntervals.last!.end = max(theIntervals.last!.end, interval.end)  
30             }
31              
32         }
33         
34         return theIntervals
35     }
36     
37     func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
38         if (intervals.count == 0) {
39             var theIntervals = intervals
40             theIntervals.append(newInterval)
41             return theIntervals
42         }
43         
44         if (newInterval.end < intervals.first!.start) {
45             var theIntervals = intervals
46             theIntervals.insert(newInterval, at: 0)
47             return theIntervals
48         }
49         
50         if (newInterval.start > intervals.last!.end) {
51             var theIntervals = intervals
52             theIntervals.append(newInterval)
53             return theIntervals
54         }
55         
56         var theIntervals = [Interval]()
57         var begin = 0
58         var end = 0
59         var insert = 0
60         var i = 0
61         while ( i < intervals.count  && intervals[i].end < newInterval.start) { 
62             theIntervals.append(intervals[i])
63             i += 1
64         }
65         
66         var theInterval = Interval(newInterval.start, newInterval.end)
67         while (i < intervals.count  && intervals[i].start <= newInterval.end) {
68             theInterval.start = min(theInterval.start, intervals[i].start)
69             theInterval.end = max(theInterval.end, intervals[i].end)
70             i += 1
71         }
72         theIntervals.append(theInterval)
73         
74         while ( i < intervals.count  && intervals[i].start > newInterval.end) { 
75             theIntervals.append(intervals[i])
76             i += 1
77         }
78         
79         return theIntervals
80     }
81 }

44ms

 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 insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
14 
15         var right = newInterval.end
16         var left = newInterval.start
17         var ans = intervals
18         for i in stride(from:ans.count - 1,through:0,by:-1) {
19             if ans[i].start > newInterval.end || ans[i].end < newInterval.start {
20                 continue
21             } else {
22                 right = max(right,ans[i].end)
23                 left = min(left,ans[i].start)
24                 ans.remove(at:i)
25             }
26         }
27         for (index,inter) in ans.enumerated() {
28             if inter.start > right {
29                 ans.insert(Interval(left,right),at:index)
30                 break
31             } else if index == ans.count - 1 {
32                 ans.append(Interval(left,right))
33             }
34         }
35         
36         if ans.count == 0 {
37             return [Interval(left,right)]
38         }
39         
40         return ans
41     }
42 }

48ms

 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 insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
14         var result = [Interval]()
15         
16         guard intervals.count>0 else { 
17             result.append(newInterval)
18             return result
19         }
20         
21         result.append(newInterval)
22         
23         for i in 0..<intervals.count {
24             var interval1 = result.removeLast()
25             var interval2 = intervals[i]
26             var cond1 = interval2.end >= interval1.start && interval2.start <= interval1.end
27             if(cond1) {
28                 var start = min(interval1.start,interval2.start)
29                 var end = max(interval1.end,interval2.end)
30                 var newInterval = Interval(start,end)
31                 
32                 result.append(newInterval)
33             }
34             else {
35                 if(interval1.start<interval2.start) {
36                     result.append(interval1)
37                     result.append(interval2)
38                 }
39                 else {
40                     result.append(interval2)
41                     result.append(interval1)
42                 }
43             }
44         }
45         
46         return result
47     }
48 }

56ms

 1 class Solution {
 2     func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
 3         var ans = [Interval]()
 4         var i = 0
 5         var insertInterval = Interval(newInterval.start,newInterval.end)
 6         while i < intervals.count {
 7             if intervals[i].end >= newInterval.start { break }
 8             ans.append(intervals[i])
 9             i += 1
10         }
11         while i < intervals.count {
12             if intervals[i].start <= newInterval.start && intervals[i].end >= newInterval.start {
13                 insertInterval.start = min(insertInterval.start,intervals[i].start)
14             }
15             if intervals[i].end >= newInterval.end && intervals[i].start <= newInterval.end {
16                 insertInterval.end = max(insertInterval.end,intervals[i].end)
17                 i += 1
18                 break
19             }
20             if intervals[i].start > newInterval.end {
21                 break
22             }
23             i += 1
24         }
25         
26         ans.append(insertInterval)
27         while i < intervals.count {
28             ans.append(intervals[i])
29             i += 1
30         }
31         
32         return ans
33     }
34 }

68ms

 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 insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
14         guard intervals.count > 0 else {
15             return [newInterval]
16         }
17         var newIntervals = [Interval](intervals)
18         newIntervals.append(newInterval)
19         newIntervals.sort{$0.start<$1.start}
20         var previousOne = newIntervals[0]
21         var result = [Interval]()
22         
23         for i in 1..<newIntervals.count {
24             let currentOne = newIntervals[i]
25             if previousOne.end >= currentOne.start {
26                 previousOne = Interval(previousOne.start, max(previousOne.end, currentOne.end))
27             } else {
28                 result.append(previousOne)
29                 previousOne = currentOne
30             }
31         }
32         
33         result.append(previousOne)
34         return result
35     }
36 }

72ms

 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     
14     func merge(_ intervals: [Interval]) -> [Interval] {
15          
16         let intervalsSorted =  intervals.sorted(by: { $0.start < $1.start } )
17       
18         var theIntervals = [Interval]()
19         
20          
21         for interval in intervalsSorted  { 
22             // no merge
23             
24             if (theIntervals.count == 0 || theIntervals.last!.end <  interval.start) { 
25                 theIntervals.append(interval) 
26             }
27             // merge
28             else {
29                 
30                  theIntervals.last!.end = max(theIntervals.last!.end, interval.end)  
31             }
32              
33         }
34         
35         return theIntervals
36     }
37     
38     func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
39         
40         var intervalsPlusOne = intervals 
41         intervalsPlusOne.append(newInterval)
42         
43         let merged = merge(intervalsPlusOne)
44         
45         return merged 
46         
47     }
48 }

76ms

 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 insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
14         guard intervals.count > 0 else {
15             return [newInterval]
16         }
17         var i = 0
18         var output: [Interval] = []
19         var newInterval = newInterval
20         var appendNew = true
21         while i < intervals.count {
22             if (intervals[i].end < newInterval.start) || (intervals[i].start > newInterval.end) {
23                 output.append(intervals[i])
24                 i += 1
25                 continue
26             }
27             var j = i
28             while j < intervals.count, !((intervals[j].end < newInterval.start) || (intervals[j].start > newInterval.end)) {
29                 appendNew = false
30                 i = j
31                 newInterval = Interval(min(intervals[j].start, newInterval.start), max(intervals[j].end, newInterval.end))
32                 j += 1
33             }
34             i += 1
35             output.append(newInterval)
36         }
37         if appendNew {
38             output.append(newInterval)
39             output.sort(by: { $0.start < $1.start })
40         }
41         return output
42     }
43 }

108ms

 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 
13 class Solution {
14     func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
15 
16         if intervals.count == 0 {
17             return [newInterval]
18         }
19         
20         var sorted = intervals
21         sorted.append(newInterval)
22         sorted.sort { (a, b) -> Bool in
23                      a.start < b.start
24                     }
25         guard var firstInterval = sorted.first else {
26             return [newInterval]
27         }
28         var result = [firstInterval]
29         var inserted = false
30         for i in 1..<sorted.count {
31             if var previous = result.last {
32                 let current = sorted[i]
33                 if self.inRange(previous, current) {
34                     result[result.count - 1] = self.merge(previous, current)
35                 } else {
36                     result.append(current)
37                 }
38             }
39         }
40         
41         return result
42     }
43     
44     func inRange(_ a: Interval, _ b: Interval) -> Bool {
45         return a.end >= b.start && a.start <= b.start
46     }
47     
48     func merge(_ first: Interval, _ last: Interval) -> Interval {
49         let start = min(first.start, last.start)
50         let end = max(first.end, last.end)
51         return Interval(start, end)
52     }
53 }

 

posted @ 2018-11-06 20:45  为敢技术  阅读(462)  评论(0编辑  收藏  举报