【Merge Intervals】cpp
题目:
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.
* 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 comp(Interval a, Interval b)
{
return a.start < b.start;
}
static vector<Interval> merge(vector<Interval>& intervals)
{
if (intervals.empty()) return intervals;
vector<Interval> ret;
std::sort(intervals.begin(), intervals.end(), Solution::comp);
int start = intervals[].start;
int end = intervals[].end;
for ( int i=; i<intervals.size(); ++i )
{
if ( intervals[i].start>end )
{
ret.push_back(Interval(start,end));
start = intervals[i].start;
end = intervals[i].end;
continue;
}
if ( intervals[i].start<=end )
{
start = std::min(start, intervals[i].start);
end = std::max(end, intervals[i].end);
continue;
}
}
ret.push_back(Interval(start,end));
return ret;
}
};
tips:
一开始理解题意有误,题中没说已经按照start对intervals排序了,所以先对intervals按照start排序(构造一个comp比较器)。接下来就是常规的思路了,每次判断当前interval的start end与之前start end的大小比较。
=========================================
还有一种解法是沿用insert interval这道题的思路,每次新插入一个interval即可,代码如下.
/**
* 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<Interval> ret;
for ( int i=; i<intervals.size(); ++i )
{
ret = Solution::insert(ret, intervals[i]);
}
return ret;
}
static vector<Interval> insert(vector<Interval>& intervals, Interval newInterval)
{
vector<Interval> ret;
int i = ;
// search for start insert position
for ( ; i<intervals.size(); ++i )
{
if ( newInterval.start > intervals[i].end )
{
ret.push_back(intervals[i]);
}
else
{
break;
}
}
// newInterval larger than all the existed intervals
if ( i==intervals.size() )
{
ret.push_back(newInterval);
return ret;
}
int start = std::min( intervals[i].start, newInterval.start );
// search for the end insert position
for ( ;i<intervals.size();++i )
{
if ( newInterval.end <= intervals[i].end ) break;
}
// newInterval end is larger than all the range
if ( i==intervals.size() )
{
ret.push_back(Interval(start, newInterval.end));
return ret;
}
if ( newInterval.end<intervals[i].start )
{
ret.push_back(Interval(start,newInterval.end));
ret.insert(ret.end(), intervals.begin()+i, intervals.end());
return ret;
}
if ( newInterval.end==intervals[i].start )
{
ret.push_back(Interval(start,intervals[i].end));
if ( i<intervals.size()- )
{
ret.insert(ret.end(), intervals.begin()+i+, intervals.end());
}
return ret;
}
if ( newInterval.end > intervals[i].start )
{
ret.push_back(Interval(start,intervals[i].end));
if ( i<intervals.size()- )
{
ret.insert(ret.end(), intervals.begin()+i+,intervals.end());
}
return ret;
}
return ret;
}
};
tips:这相当于是对一个区间进行插入排序,之前做的都是针对数字进行插入排序。这道题考察的点还是很好的。
===============================================
第二次过这道题,就是先按照start进行排序,再merge。
/**
* 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 a, Interval b)
{
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals)
{
vector<Interval> ret;
if ( intervals.empty() ) return ret;
sort(intervals.begin(), intervals.end(), Solution::compare);
ret.push_back(*intervals.begin());
for ( vector<Interval>::iterator i=intervals.begin()+; i!=intervals.end(); ++i )
{
if ( i->start > ret.back().end )
{
ret.push_back(*i);
}
else
{
ret.back().start = min(ret.back().start, i->start);
ret.back().end = max(ret.back().end, i->end);
}
}
return ret;
}
};
【Merge Intervals】cpp的更多相关文章
- 【Insert Interval】cpp
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 【Reorder List】cpp
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Multiply Strings】cpp
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 排序算法总结(二)归并排序【Merge Sort】
一.归并排序原理(Wikipedia) 归并排序本质是分治思想的应用,并且各层分治递归可以同时进行 1.申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列 2.设定两个指针,最初位置 ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
随机推荐
- April 25 2017 Week 17 Tuesday
Have you ever known the theory of chocie? There are a bunch of axiems, but there are only two thing ...
- ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id
远程删除key ssh-keygen -f "~/.ssh/known_hosts" -R 192.168.0.34 如果还是不可以,通过 ssh-keygen 重新生成key
- 认识http客户端
最简单的http客户端就是我们的浏览器,浏览器地址输入baidu.com,就会返回响应内容,打开network,都是http请求,第一个就是www.baidu.com的请求,旁边第一个General就 ...
- 怎样在 Ubuntu Linux 上安装 MySQL
本教程教你如何在基于 Ubuntu 的 Linux 发行版上安装 MySQL.对于首次使用的用户,你将会学习到如何验证你的安装和第一次怎样去连接 MySQL. -- Sergiu MySQL 是一个典 ...
- sublime text 3 python 控制台输出中文乱码解决方案
自建的python运行环境如下:python3 找到python3.sublime-build文件打开,在文件中加入"env": { "PYTHONIOENCODING& ...
- wcf 的小介绍
http://www.cnblogs.com/scottckt/archive/2010/10/15/1852136.html
- 第49章 在SRAM中调试代码—零死角玩转STM32-F429系列
第49章 在SRAM中调试代码 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- vue 城市搜索组件
1.实现大致是如下效果 2.搜索组件的页面结构 <template> <div> <div class="search"> ...
- git(osChina上分支的使用)
使用osChina分支的创建分为两种 1.直接在osChina上创建 需要pull否则查看git的状态是不包含改分支的; git pull <git地址/git简称> <分支名> ...
- Webpack4 学习笔记三 ES6+语法降级为ES5
前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 Webpack 将es6.es7语法降级为es5 需要通过 babel JavaScript编译器. 安装: npm i babel ...