【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].
记返回数组为ret。
先对start排序。
然后对每两个interval(记为a,b),判断是否需要合并。
如果不需要合并(没有交集),则把a装入ret,将b继续往后。
如果需要合并(有交集),则把结果c继续往后。
这题本身是不难的,但是有两个细节:
1、compare函数中,如果是升序,必须是<而不是<=
解释:参考http://www.cplusplus.com/reference/list/list/sort/,比较必须产生strick weak ordering。
对于strick weak ordering 可以参考http://stackoverflow.com/questions/979759/operator-and-strict-weak-ordering/981299#981299
的详细说明。
总之,如果a,b不等,那么compare(a,b)和compare(b,a)其中之一为true,另一为false。
如果a,b相等,则都应该为false。
2、compare函数必须声明为静态成员函数或者全局函数,不能作为普通成员函数
解释:非静态成员函数是依赖于具体对象的,而std::sort这类函数是全局的,因此无法在sort中调用非静态成员函数。
可以把compare改成静态或者全局函数,使之不依赖于具体对象。
/**
* 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:
static bool compare(Interval v1, Interval v2)
{
if(v1.start < v2.start)
return true;
else if(v1.start > v2.start)
return false;
else
return v1.end < v2.end;
}
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
if(intervals.empty())
return ret; sort(intervals.begin(), intervals.end(), compare);
ret.push_back(intervals[]);
for(int i = ; i < intervals.size(); i ++)
{//merge intervals to ret one-by-one //note that endv.start <= intv.start
if(intervals[i].end <= ret[ret.size()-].end)
//totally enclosed
;
//to here: intv.end > endv.end
else if(intervals[i].start <= ret[ret.size()-].end)
//merge
ret[ret.size()-].end = intervals[i].end;
//to here: intv.begin > endv.end
else
ret.push_back(intervals[i]);
}
return ret;
}
};

【LeetCode】56. Merge Intervals的更多相关文章
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#56. Merge Intervals
一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- LeetCode 题解 56. Merge Intervals
题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...
- LeetCode OJ 56. Merge Intervals
题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...
- [leetcode sort]56. Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
随机推荐
- List 集合转换为String
开发中会用到把 List<string> 的内容拼接成以逗号分隔的字符串的形式,现总结如下: 方法一: public String listToString(List list, cha ...
- 用java解析在OpenStreetMap上下载的地图数据
采用dom4j解析下载的xml文件,java程序如下: package gao.map.preprocess; import java.io.BufferedWriter; import java.i ...
- [leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...
- [Algorithm] Universal Value Tree Problem
A unival tree (which stands for "universal value") is a tree where all nodes under it have ...
- (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 t ...
- 【问题&解决】还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员”的解决办法
今天在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: ...
- 理解进程调度时机跟踪分析进程调度与进程切换的过程(Linux)
----------------------------------------------------------------------------------- 理解进程调度时机跟踪分析进程调度 ...
- 自定义cas客户端核心过滤器AuthenticationFilter
关于cas客户端的基本配置这里就不多说了,不清楚的可以参考上一篇博文:配置简单cas客户端.这里是关于cas客户端实现动态配置认证需要开发说明. 往往业务系统中有些模块或功能是可以不需要登录就可以访问 ...
- PD的CDM模型中的三种实体关系
PD的CDM模型中的三种实体关系 本文摘自:http://www.cnblogs.com/syf/articles/2480580.html PD 正向工程使用说明:http://download.c ...
- Node使用淘宝 NPM 镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org之后可以通过cnpm来安装node模块cnpm install [name]