leetcode-56. Merge Intervals 合并区间

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 considered 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] 可被视为重叠区间。

思路:先对按照区间的左半部分排序,然后数据就变为在左半部分有序并且 右半部分也 有序。这样顺序往后遍历,若遇到未重叠情况,res.back().end

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    vector merge(vector& intervals) {
        if(intervals.empty()) return {};   
        sort(intervals.begin(),intervals.end(),[](Interval &a, Interval &b) {return a.start < b.start;});
        vector res{intervals[0]};
        for(int i=1;i

 

你可能感兴趣的:(数据结构/算法/刷题,#)