代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4
 5 using namespace std;
 6
 7 struct Interval {
 8     int start;
 9     int end;
10     Interval() : start(0), end(0) {}
11     Interval(int s, int e) : start(s), end(e) {}
12 };
13
14 bool compare(Interval &a, Interval &b)
15 {
16     return a.start < b.start;
17 }
18
19 vector<Interval> merge(vector<Interval>& intervals)
20 {
21     vector<Interval> result;
22     sort(intervals.begin(), intervals.end(), compare);
23     for (int i = 0; i < intervals.size()-1; i++)
24     {
25         if ((i+1!=intervals.size()-1)&&intervals[i].end >= intervals[i + 1].start)
26         {
27             intervals[i].end = intervals[i + 1].end;
28         }
29         else
30         {
31             result.push_back(intervals[i]);
32         }
33     }
34     return result;
35 }
36
37
38 int main()
39 {
40     Interval I1(1,3);
41     Interval I2(2,6);
42     Interval I3(8, 10);
43     Interval I4(15,18);
44     vector<Interval> itervals;
45     itervals.push_back(I1);
46     itervals.push_back(I2);
47     itervals.push_back(I3);
48     itervals.push_back(I4);
49
50     vector<Interval> result = merge(itervals);
51     for (int i = 0; i < result.size(); i++)
52     {
53         cout << result[i].start << "    " << result[i].end << endl;
54     }
55 }

转载于:https://www.cnblogs.com/chaiwentao/p/4646651.html

Merge Intervals相关推荐

  1. [LeetCode]Merge Intervals

    题目:Merge Intervals 给定n个区间合并重合区间 思路: 先按照区间起点排序,然后合并下面情况: 1.起点相同,以最大的终点为新的终点: 2.前一个终点大于后一个的起点. /****** ...

  2. 【leetcode】56. Merge Intervals 相邻线段归并

    1. 题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2, ...

  3. C#LeetCode刷题之#56-合并区间(Merge Intervals)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3676 访问. 给出一个区间的集合,请合并所有重叠的区间. 输入: ...

  4. 【细节实现题】LeetCode 56. Merge Intervals

    LeetCode 56. Merge Intervals Solution1:我的答案 这道题思路不难,有个坑爹的地方在于输入的区间向量未必是有序的,所以利用优先队列priority_queue来排序 ...

  5. 56. Merge Intervals(合并区间)解法(C++ 注释)

    56. Merge Intervals(合并区间) 1. 题目描述 2. 排序(Sorting) 2.1 解题思路 2.2 实例代码 1. 题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 ...

  6. LeetCode 56. Merge Intervals

    原题链接在这里:https://leetcode.com/problems/merge-intervals/ 题目: Given a collection of intervals, merge al ...

  7. LeetCode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  8. LeetCode:Merge Intervals

    题目链接 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6 ...

  9. merge intervals(合并间隔)

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

最新文章

  1. 双方互GAN,不如来试试群体博弈?更快更强更自由 | ICLR 2021
  2. C/Cpp / 设计模式 / 模板模式
  3. 发布与订阅消息--Redis学习笔记八
  4. PESCMS Ticket 客服工单系统 v1.2.4 发布
  5. 这里有一份面筋请查收(八)
  6. Java入门算法(滑动窗口篇)丨蓄力计划
  7. Operations Manager 2007 监控Active Directory SCOM-Part 3
  8. Spring框架学习笔记09:基于XML配置方式搭建SSM框架实现用户登录
  9. 江浙沪地区计算机考研高效排名,京津冀,江浙沪地区院校盘点!
  10. 如何判断一个数为无符号数还是有符号数
  11. 苹果Mac Win10式任务栏工具:uBar
  12. 5.linux设备驱动模型
  13. 关于在Ubuntu中更新pip时遇到的问题及解决方法
  14. 1.13 08特殊日历计算
  15. Centos7环境启动mongod报polkit服务启动失败
  16. Python中利用pygame做弹球游戏
  17. 烤仔TVのCCW | 智能合约间的四种调用(下)
  18. 基本保险金额和保额的意思和区别是什么?
  19. Lead-follower因子:新闻共现股票收益的关联性研究
  20. ubuntu服务器上,求生之路2服务器如何把监听地址改为0.0.0.0

热门文章

  1. 跳水比赛背后的隐形教练现身了!百度智能云还完成了历史性的大升级
  2. 咱们程序员,能吵吵就别动手!现在靠说就可以编程,支持Java、Python等10种语言|免费...
  3. 拼多多高速冲刺8亿用户新时代 黄峥卸任董事长 探索行业未来十年发展
  4. 实战解析:真实AI场景下,极小目标检测与精度提升 | 百度AI公开课
  5. 沙发变身遥控器,涂鸦里藏PCB,MIT技术宅的智能家居竟然是这样
  6. 一片哗然!ICLR 2020被曝47%的审稿人没发过相关论文
  7. Go 语言:The Laws of Reflection 中文版
  8. linux中注册系统服务—service命令的原理通俗
  9. 企业部署Linux应用将拥有更低的整体拥有成本
  10. 设置Eclipse编码方式