19.2.8 [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 bool operator <(const Interval& a, const Interval& b) {
 2     if (a.start == b.start)
 3         return a.end > b.end;
 4     return a.start < b.start;
 5 }
 6
 7 class Solution {
 8 public:
 9     vector<Interval> merge(vector<Interval>& intervals) {
10         if (intervals.size()<=1)return intervals;
11         set<Interval>q;
12         while (!intervals.empty()) {
13             Interval now = intervals.back();
14             intervals.pop_back();
15             q.insert(now);
16         }
17         auto pre = q.begin();
18         int mm = (*pre).end;
19         for (auto p = q.begin(); p != q.end(); ) {
20             if (p != q.begin() && (*p).start <= (*pre).end) {
21                 mm = max((*p).end, mm);
22                 auto oldp = p;
23                 p++;
24                 q.erase(oldp);
25             }
26             else if (p != q.begin()) {
27                 int star = (*pre).start;
28                 q.erase(pre);
29                 q.insert(Interval(star, mm));
30                 if ((*p).start <= mm)
31                     pre = q.find(Interval(star, mm));
32                 else {
33                     pre = p;
34                     p++;
35                 }
36                 mm = (*pre).end;
37             }
38             else p++;
39         }
40         if ((*pre).end < mm) {
41             q.insert(Interval((*pre).start, mm));
42             q.erase(pre);
43         }
44         return vector<Interval>(q.begin(),q.end());
45     }
46 };

原文地址:https://www.cnblogs.com/yalphait/p/10356600.html

时间: 2024-11-13 08:40:47

19.2.8 [LeetCode 56] Merge Intervals的相关文章

LeetCode --- 56. Merge Intervals

题目链接:Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路:题意很明确,首先对各区间按开始来排序,最后遍历,如果前面和后面的区间有重合,合并. 具体代码: /** * Definition for an interval. * publi

LeetCode 56. Merge Intervals (合并区间)

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序

[LeetCode] 56. Merge Intervals 解题思路

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 问题:给定一个区间集合,整合所有重叠的区间. 对区间集合按照 start 来排序,然后根据 intervals[i].start 和 res.lastElement.end 来整合即可. 1 int static com

[LeetCode] 56. Merge Intervals Java

题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题意及分析:给出很多个区间,合并有重叠的区间,输出最后的结果.我们分析可以得知对于两个区间interval1和interval2,如果interval1.start<=interval2.start,有一下几种情

[LeetCode] 56 - Merge Intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 思路: 我们首先要做的就是给区间集排序,由于我们要排序的是个结构体,所以我们要定义自己的comparator,才能用sort来排序,我们以start的值从小到大来排序,排完序我们就可以开始合并了,首先把第一个区间存入结果

56. Merge Intervals - LeetCode

Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并 Java实现: public List<Interval> merge(List<Interval> intervals) { if (intervals == null || intervals.size() == 0) return i

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare 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, mer

No.56 Merge Intervals

No.56 Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3