感觉有一点进步了,但是思路还是不够犀利。

/*** 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<Interval> merge(vector<Interval>& intervals) {vector<pair<int,int>> r;for(int i=0;i<intervals.size();i++)r.push_back(make_pair(intervals[i].start,intervals[i].end));sort(r.begin(),r.end());int index=0;vector<Interval> res;while(index<r.size()){int start=r[index].first;int end=r[index].second;if(start == INT_MAX){index++;continue;}for(int i=index+1;i<r.size();i++){if(r[i].first == end){end=r[i].second;r[i].first=INT_MAX,r[i].second=INT_MAX;}}while(r[index+1].first >= start && r[index+1].second<=end && r[index+1].second>=start&&r[index+1].second<=end){index++;}while(end>=r[index+1].first && end<=r[index+1].second && index+1 <=r.size()){index++;end=r[index].second;}res.push_back(Interval(start,end));index++;}return res;}
};

  本地测试正确,不知道哪里有问题

#include"stdafx.h"
#include<iostream>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;struct Interval {int start;int end;Interval() : start(0), end(0) {}Interval(int s, int e) : start(s), end(e) {}};
class Solution {
public:vector<Interval> merge(vector<Interval>& intervals) {auto next = intervals.begin();auto pre = next++;while (next != intervals.end()) {if (pre->end >= next->start) {pre->end = next->end;next = intervals.erase(next);}else {pre++;next++;}}return intervals;}
};int main()
{vector<Interval> coll;Interval a =  Interval(1, 3);Interval b = Interval(2, 6);Interval c = Interval(8, 10);Interval d = Interval(15, 18);coll.push_back(a);coll.push_back(b);coll.push_back(c);coll.push_back(d);
//  coll.erase(coll.begin());Solution s;s.merge(coll);for (auto i : coll)cout << i.start << " " << i.end << endl;system("pause");return 0;
}

  

转载于:https://www.cnblogs.com/yanqi110/p/5013454.html

LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。相关推荐

  1. [LeetCode]Merge Intervals

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

  2. [leetcode]Merge Intervals @ Python

    原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...

  3. LeetCode Merge Intervals

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

  4. LeetCode:Merge Intervals

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

  5. LeetCode - Merge Intervals

    02.08.2021 小白一枚,文章仅当日记本记录学习进度 ;) 如被浏览,请多多指教,非常感谢大家纠错与建议! (因留学顺便练习英语,所以部分用英文笔记,并无他意) Solution 1 - sor ...

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

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

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

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

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

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

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

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

  10. 我的眼睛疼,脑袋疼,想睡觉。。。。。。 :(

    我的眼睛疼,脑袋疼,想睡觉,浑身不舒服,搞这行真不好.以后一定要换行. 还是身体最重要.

最新文章

  1. 深入理解PHP内核(五)函数的内部结构
  2. python集合(set)+常见方法+交叉并补
  3. 黄雷:展开数据的翅膀,在更宽广的路上飞驰人生 | 提升之路系列(三)
  4. anaconda3配置环境变量_Python:Anaconda安装及LabelMe配置(1)
  5. IIS设定 CORS 跨域请求(跨域)
  6. scrapy的name变量_python-将file_name参数传递给管道以在scrapy中...
  7. 指针、数组、函数阶段小结
  8. python 多继承 __new___Python3中的__new__方法以及继承不可变类型类的问题
  9. [Godot]使用精灵集的时候要注意关闭过滤器
  10. SRM 542 DIV2
  11. SpringBoot 整合swagger3.X
  12. Vue:Vue项目中引入第三方库报错Unexpected token ‘<‘
  13. 基于asp.net C#中小型超市库存管理系统
  14. 在繁杂的网页中揪出email地址
  15. 计算年龄:sql计算
  16. 将数字转化为拼音(1000内)
  17. mysql参数化查询 in_mysql in 查询参数化
  18. 【C语言编译器】01程序-编译器-IDE
  19. Transform 3.1 用户手册(SPSS 的通用数据转换程序)
  20. 秒懂设计模式之组合模式(Composite Pattern)

热门文章

  1. iOS开发经验总结(二)
  2. Compressor 4.6.1 for Mac(视频转码工具)
  3. Parallels Desktop 17 for Mac(pd虚拟机)支持Intel 和 Apple M1
  4. 如何在M1上运行较早的非本机Intel x86应用
  5. Mac如何简单的翻录3D蓝光视频
  6. 第十三章:位图(八)
  7. Android高效率编码-第三方SDK详解系列(三)——JPush推送牵扯出来的江湖恩怨,XMPP实现推送,自定义客户端推送...
  8. 2013年7月9日“新单位工作流水账之一”
  9. python入门(1)文档的处理
  10. 安装win7的小插曲