LeetCode(57) Insert Interval
题目
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].
分析
与上一题本质相同,只需要将给定元素插入原 vector,然后将LeetCode 56题代码执行一遍即可!
AC代码
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
//自定义Interval类型元素的升序比较函数
bool cmp(Interval a, Interval b)
{
return a.start < b.start;
}
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
//如果输入参数为空,则返回空vector
if (intervals.empty())
return vector<Interval>(1 , newInterval);
//将新元素插入序列中
intervals.push_back(newInterval);
int len = intervals.size();
//首先,按照每个Integerval的区间首值进行排序,自定义比较
sort(intervals.begin(), intervals.end(), cmp);
//声明结果
vector<Interval> ret;
//定义临时变量
Interval temp = intervals[0];
for (int i = 0; i < len; i++)
{
//换一种判断方法
if (intervals[i].start > temp.end)
{
ret.push_back(temp);
temp = intervals[i];
}
else{
temp.end = temp.end > intervals[i].end ? temp.end : intervals[i].end;
}//else
}//for
ret.push_back(temp);
return ret;
}
};
LeetCode(57) Insert Interval的更多相关文章
- LeetCode(57):插入区间
Hard! 题目描述: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: i ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 用直接路径(direct-path)insert提升性能的两种方法
1.传统串行insert方式 常见的insert方式有两种: (1) insert into table_name values(....) (2) insert into target_table ...
- Flash Builder 调试器无法连接到正在运行的应用程序(57%)
Flash Builder 调试器无法连接到正在运行的应用程序(57%),可能原因: 1,flashplayer不是debug版. 2,调试器(用debug版flashplayer随便 ...
- Qt 学习之路 2(57):可视化显示数据库数据
Qt 学习之路 2(57):可视化显示数据库数据(skip) 豆子 2013年6月26日 Qt 学习之路 2 26条评论 前面我们用了两个章节介绍了 Qt 提供的两种操作数据库的方法.显然,使用QSq ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- jQuery笔记之热点搜索排名小demo
先来看一下成品图: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- AFNetworking https自签名证书 -1012 解决方案
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy]; //是否信任服务器无效或过期的SSL证书.默认为“不”. se ...
- 2019 年 Vue 学习路线图!
如果你是 Vue 开发新手,可能已经听过很多行话术语,比如单页面应用程序.异步组件.服务器端渲染,等等.你可能还听说过与 Vue 有关的一些工具和库,比如 Vuex.Webpack.Vue CLI 和 ...
- Web | 解决中文乱码
设定文件的编码格式在head中添加 <head> <meta http-equiv="Content-Type" content="text/html; ...
- the little schemer 笔记(3)
第三章 cons the magnificent (rember a lat)是什么,其中a是mint,lat是(lamb chops and mint jelly) (lamb chops and ...
- Django framework
1. Django 的内置web server是如何实现的 2. Django 的WSGI是如何实现的 3. Django middle ware是如何实现的 4. Django framework的 ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
- markdown快捷键(转)
markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...
- re匹配语法-match、search和findall
1.re.match() 匹配第一个值 列表里的值可以有多个范围,有一个符合就可以. match只匹配第一个值,所以列表里的范围是第一个值得取值范围.如果第一个值被设定好且存在,那么列表的取值范围变为 ...
- JS进阶-闭包的几种常见形式
作用域链: //作用域链 var a = 1; function test() { var b =2; return a; } alert(test());//弹出1: alert(b);//不能获取 ...