[LeetCode] Merge Intervals

简介: The idea to solve this problem is to first sort the intervals according to their start field and then scan the intervals from head to tail and merge those that overlap.

The idea to solve this problem is to first sort the intervals according to their start field and then scan the intervals from head to tail and merge those that overlap.

For sorting the intervals according to their start field, we define a comparison function as follows.

1 static bool comp(Interval interval1, Interval interval2) {
2     return interval1.start < interval2.start;
3 }

Then all the intervals are sorted in the ascending order of start. Now we define a current intervalcur and initialize it to be intervals[0]. Then we scan from intervals[1] to intervals[n - 1]. If it overlaps with cur, merge them; otherwise, add cur to res, update cur to be intervals[i]and move on with the merging process.

There are two required subroutines in the above process: isOverlap to tell whether two intervals overlap and mergeTwo to merge two overlapping intervals.

For isOverlap, since the intervals are sorted in ascending order of start, we simply need to guarantee that end of the left (with smaller start) interval is not less than start of the right (with larger start) interval.

For mergeTwo, just take the minimum of start and maximum of end of the two overlapping intervals and return a new interval with these two values.

The complete code is as follows, which should be self-explanatory.

 1 class Solution {
 2 public:
 3     vector<Interval> merge(vector<Interval>& intervals) {
 4         vector<Interval> res;
 5         if (intervals.empty()) return res;
 6         sort(intervals.begin(), intervals.end(), comp);
 7         Interval cur(intervals[0].start, intervals[0].end);
 8         for (int i = 1, n = intervals.size(); i < n; i++) {
 9             if (isOverlap(cur, intervals[i]))
10                 cur = mergeTwo(cur, intervals[i]);
11             else {
12                 res.push_back(cur);
13                 cur = intervals[i];
14             }
15         }
16         res.push_back(cur);
17         return res;
18     }
19 private:
20     static bool comp(Interval interval1, Interval interval2) { 
21         return interval1.start < interval2.start;
22     }
23     bool isOverlap(Interval interval1, Interval interval2) {
24         return interval1.end >= interval2.start;
25     }
26     Interval mergeTwo(Interval interval1, Interval interval2) {
27         int start = min(interval1.start, interval2.start);
28         int end = max(interval1.end, interval2.end);
29         return Interval(start, end);
30     }
31 };

 

目录
相关文章
|
存储
LeetCode 352. Data Stream as Disjoint Intervals
给定一个非负整数的数据流输入 a1,a2,…,an,…,将到目前为止看到的数字总结为不相交的区间列表。
101 0
LeetCode 352. Data Stream as Disjoint Intervals
LeetCode 88. Merge Sorted Array
题意是给定了两个排好序的数组,让把这两个数组合并,不要使用额外的空间,把第二个数组放到第一个数组之中.
90 0
LeetCode 88. Merge Sorted Array
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
人工智能 搜索推荐
LeetCode 56. Merge Intervals
给定间隔的集合,合并所有重叠的间隔。
98 0
Leetcode-Easy21. Merge Two Sorted Lists
Leetcode-Easy21. Merge Two Sorted Lists
111 0
Leetcode-Easy21. Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
110 0
|
Java Python
LeetCode 21:合并两个有序链表 Merge Two Sorted Lists
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解题思路: 迭代和递归都能解题。
1003 0
|
Java
[LeetCode]Merge Sorted Array 合并排序数组
链接:https://leetcode.com/problems/merge-sorted-array/description/难度:Easy题目:88.
1031 0