【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
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]
.
思路:
Insert Interval是Merge Intervals的一个延伸问题,先看看怎么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]
.
- vector<Interval> merge(vector<Interval> &intervals) {
- if(intervals.size() <= )
- return intervals;
- vector<Interval> vres;
- sort(intervals.begin(), intervals.end(), intvalcomp);//先对interval排序
- Interval tmp(intervals[]);
- for(Interval it:intervals){
- if(tmp.start == it.start){
- tmp.end = it.end;
- }else if(tmp.end >= it.start){//intervals有序,必然有tmp.start < it->start
- if(tmp.end < it.end)//直接无视后者{[1,4],[2,3]}
- tmp.end = it.end;//直接吞并后者{[1,3],[2,4]}
- }else{
- vres.push_back(tmp);//不相交
- tmp = it;
- }
- }
- vres.push_back(tmp);//漏掉这句会fail{[1,4],[1,4]}
- return vres;
- }
- bool intvalcomp(Interval a, Interval b){
- if(a.start == b.start)
- return a.end < b.end;
- else
- return a.start < b.start;
- }
现在有了这个Merge好了的不相交区间序列,怎么进行插入呢?Insert Interval条件太多,每一个大小等号比较,每一个小下标就能让人栽跟斗,因此它也是我目前最讨厌的题目,没有之一。
一开始尝试这种思路:
“新序列按照start排好序(start肯定是各不相同的),第一步我们先用二分找出有交集的序列片段的开始,这一点很像Search Insert Position,然后再往后处理。”
脑子不清楚憋了一下午,恶心的我两天不能刷Leetcode,如果真要写出来的话,就老老实实下面这样,效率不一定差,因为看题目反正是不想要你改变输入参数,横竖都得遍历一遍来拷贝。挺有意思的是,晚上我看到了Google Campus的youku视频,讲述的就是一个倒霉孩子花了30min写二分Insert Interval的反例。。。
- vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
- vector<Interval> rs;
- int i = ;
- while(i < intervals.size() && intervals[i].end < newInterval.start){//找到第一个起点
- rs.push_back(intervals[i++]);
- }
- if(i == intervals.size()){//为空或过了结尾点
- rs.push_back(newInterval);
- return rs;
- }
- newInterval.start = min(newInterval.start, intervals[i].start);
- while(i < intervals.size() && intervals[i].start <= newInterval.end){//找到结束点
- newInterval.end = max(newInterval.end, intervals[i++].end);
- }
- rs.push_back(newInterval);
- while(i < intervals.size()){
- rs.push_back(intervals[i++]);
- }
- return rs;
- }
【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals的更多相关文章
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- [leetcode]Insert Interval @ Python
原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
随机推荐
- input固定定位后,当input框获取到焦点时,会离开手机软键盘的解决方法
前些天做页面时候遇到这样一个问题,将input框用position:fixed固定定位在最底部的时候,当Input获取焦点的时候,input框离开了手机软键盘,而不是吸附在软键盘上,效果如下图: 找了 ...
- mysql 远程连接速度慢的解决方案
PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的MYSQL开启了DNS的反向解析,在MY.INI(WINDOWS ...
- MyEclipse 修改代码不生效
最近得了一个项目,java开发的web项目,修改代码时,无论怎么改,都不生效: 各种度娘,没用. 原因是没有建立发布设定 这个东西我开始不理解它的作用,现在知道了: mysqleclipse项目在一个 ...
- hdu 4614 Vases and Flowers
http://acm.hdu.edu.cn/showproblem.php?pid=4614 直接线段树维护 代码: #include<iostream> #include<cstd ...
- Codeforces Round #257 (Div. 1) (Codeforces 449D)
思路:定义f(x)为 Ai & x==x 的个数,g(x)为x表示为二进制时1的个数,最后答案为 .为什么会等于这个呢:运用容斥的思想,如果 我们假设 ai&x==x 有f(x ...
- python 操作json
认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...
- [开发笔记]-js判断用户的浏览设备是移动设备还是PC
最近做的一个网站页面中需要根据用户的访问设备的不同来显示不同的页面样式,主要是判断移动设备还是电脑浏览器访问的. 下面给出js判断处理代码,以作参考. <script type="te ...
- Rhel6-tomcat+nginx+memcached配置文档
理论基础: User - > web ->nginx ->tomcat1 ->*.jsp 80 8080 ↓ -> tomcat2 html ...
- 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- android 录音的断点续传
系统没有暂停的功能 只能把每次的录音进行拼接... package com.example.zrecord; import java.io.File;import java.io.FileInput ...