56. Merge Intervals 57. Insert Interval *HARD*
1. Merge
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 compare(Interval a, Interval b)
{
if(a.start != b.start)
return a.start < b.start;
return a.end < b.end;
}
vector<Interval> merge(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), compare);
vector<Interval> v;
int n = intervals.size(), i;
if(n<)
return v;
for(i = ; i < n; i++)
{
if(v.size() && intervals[i].start <= v[v.size()-].end)
v[v.size()-].end = max(intervals[i].end, v[v.size()-].end);
else
v.push_back(intervals[i]);
}
return v;
}
};
2. Insert
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
(1) 用上面的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)
{
if(a.start != b.start)
return a.start < b.start;
return a.end < b.end;
}
vector<Interval> merge(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), compare);
vector<Interval> v;
int n = intervals.size(), i;
if(n<)
return v;
for(i = ; i < n; i++)
{
if(v.size() && intervals[i].start <= v[v.size()-].end)
v[v.size()-].end = max(intervals[i].end, v[v.size()-].end);
else
v.push_back(intervals[i]);
}
return v;
}
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
intervals.push_back(newInterval);
return merge(intervals);
}
};
(2)
/**
* 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> insert(vector<Interval>& intervals, Interval newInterval) {
int n = intervals.size(), l, i;
vector<Interval> v;
for(i = ; i < n; i++)
{
if(newInterval.start > intervals[i].end)
v.push_back(intervals[i]);
else
break;
}
if(i >= n)
{
v.push_back(newInterval);
return v;
}
l = v.size();
v.push_back(intervals[i]);
if(newInterval.start < v[l].start)
v[l].start = newInterval.start;
while(i < n && newInterval.end > intervals[i].end)
i++;
if(i<n && newInterval.end >= intervals[i].start)
v[l].end = intervals[i++].end;
else
v[l].end = newInterval.end;
while(i<n)
v.push_back(intervals[i++]);
return v;
}
};
56. Merge Intervals 57. Insert Interval *HARD*的更多相关文章
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- [VTK]基于VTK的三维重建
https://www.cnblogs.com/dawnWind/archive/2013/02/17/3D_06.html 0. Background 很久很久以前记录了一下使用WPF进行三维重建的 ...
- http协议/获得请求/中文参数处理/访问数据库
# 1. http协议(了解)## (1)什么是http协议?一种网络应用层协议,规定了浏览器与web服务器之间如何通信以及相应的的数据包的结构.注:tcp/ip协议:保证数据可靠的传递.(UDP不可 ...
- JS引擎深入分析
转载自阮一峰:http://javascript.ruanyifeng.com/bom/engine.html 目录 JavaScript代码嵌入网页的方法 script标签:代码嵌入网页 scrip ...
- JS事件监听器
JS事件监听器 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Javasc ...
- MySQL 5.7.18 解压版安装
原文链接:https://my.oschina.net/u/3474266/blog/895696 我在安装免安装版的5.7.18的时候出现了问题,正好找到这个,十分感激 今天下载安装了MySQL C ...
- 使用CLR Profiler查看C#运行程序的内存占用情况
http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...
- 同步TreeView中父结点和子结点的状态[以及Treeview的bug]
此代码仅仅是二级结点,即父结点下面只有一层子结点,只有两层结构 /// <summary> /// 某个结点的CheckBox被选中 /// </summary> /// &l ...
- 51NOD 1066 Bash游戏
1066 Bash游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的 ...
- 论文笔记之:End-to-End Localization and Ranking for Relative Attributes
End-to-End Localization and Ranking for Relative Attributes arXiv Paper 摘要:本文提出一种 end-to-end 的属性识别方 ...
- Linux下替换默认版本的protobuf
1. 下载 下载地址,我下载的是protobuf-all-3.5.0.tar.gz,尽量下载all,里面包含protobuf库的所有文件. 2. 编译 ./configure make make in ...