题目

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;
}
};

GitHub测试程序源码

LeetCode(57) Insert Interval的更多相关文章

  1. LeetCode(57):插入区间

    Hard! 题目描述: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: i ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. 用直接路径(direct-path)insert提升性能的两种方法

    1.传统串行insert方式 常见的insert方式有两种: (1) insert into table_name values(....) (2) insert into target_table ...

  4. Flash Builder 调试器无法连接到正在运行的应用程序(57%)

    Flash Builder 调试器无法连接到正在运行的应用程序(57%),可能原因:     1,flashplayer不是debug版.     2,调试器(用debug版flashplayer随便 ...

  5. Qt 学习之路 2(57):可视化显示数据库数据

    Qt 学习之路 2(57):可视化显示数据库数据(skip) 豆子 2013年6月26日 Qt 学习之路 2 26条评论 前面我们用了两个章节介绍了 Qt 提供的两种操作数据库的方法.显然,使用QSq ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. 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 ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. JQuery动态添加表格,然后动态删除不成功问题

    背景: 自己做了一个测试网页,想动态添加表格,然后删除,按照网上的教程写完,发现点击"删除参数"按钮没用 源码: function addtr() { var trinfo = & ...

  2. laravel之Storage

    学习laravel一段时间了,从最开始文件保存到本地直到oss都使用原生或接口.今天偶尔看到了Storage.正好项目中有用到就在项目上使用了. 1.下载安装库 composer require ja ...

  3. AtCoder Regular Contest 062 E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer

    题目传送门:https://arc062.contest.atcoder.jp/tasks/arc062_c 题目大意: 给你\(N\)块正方形木板,每块木板四角有四种颜色(可以相同),木板中央有编号 ...

  4. 从一个n位数中选出m位按顺序组成新数并使其最大 || Erasing and Winning UVA - 11491

    就是从n位数中取出n-d个数字按顺序排成一排组成一个新数使得其最大 算法: 从前往后确定每一位.找第i位时,要求后面留下d-i位的空间, 因此第i位应该从第i-1位原来位置+1到第d+i位寻找 用线段 ...

  5. E. Anton and Tree 数组开大点

    http://codeforces.com/contest/734/problem/E 看了题解,缩点 + 树的直径. 然而一直wa14. 注意到, 缩点后重建图,在5的时候,5和6建了一条边,然后6 ...

  6. web简单的整体测试

    网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题 ab测试 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它 ...

  7. POST 传参

    $http.post("../jzgCar_listAllJzgCar.do?data={parentId:"+value+"}") value 是参数,都是双 ...

  8. jmeter配置mysql数据库步骤

    安装环境: Windows10系统 jmeter版本:3.0版本 java1.8版本 安装步骤: 1.下载连接mysql数据库jar包,地址:http://download.csdn.net/deta ...

  9. img 标签访问图片返回403forbidden

    做百度编辑器时,从秀米复制过来的文档,图片不无法加载,返回403的错 解决办法 解决这个问题只需要在头部添加一个meta <meta name="referrer" cont ...

  10. Java垃圾回收机制分析

    Java的堆是一个运行时数据区,类的实例从中分配空间,堆中存储着正在运行的应用程序所建立的所有对象.垃圾回收是一种动态存储管理技术.它按照特定的垃圾回收算法,自动释放掉不再被引用的对象.堆内存里垃圾的 ...