Skip to content

Commit 61435da

Browse files
committed
Create 0056-merge-intervals.swift
1 parent 8f61c2d commit 61435da

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

swift/0056-merge-intervals.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/merge-intervals/
3+
*/
4+
5+
class Solution {
6+
func merge(_ intervals: [[Int]]) -> [[Int]] {
7+
var intervals = intervals.sorted { $0[0] < $1[0] }
8+
var res = [intervals[0]]
9+
10+
for interval in intervals {
11+
let start = interval[0]
12+
let end = interval[1]
13+
let lastEnd = res[res.count - 1][1]
14+
15+
if start <= lastEnd {
16+
res[res.count - 1][1] = max(lastEnd, end)
17+
} else {
18+
res.append([start, end])
19+
}
20+
}
21+
22+
return res
23+
}
24+
}

0 commit comments

Comments
 (0)