[LeetCode] 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]
.
#include <vector>
#include <iostream>
using namespace std;
/**
* Definition for an interval.
*/
struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
}; class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
int n = intervals.size();
if(n<) return vector<Interval>{{newInterval.start,newInterval.end}};
vector<Interval> ret;
// if(n==1){
// if(newInterval.end<intervals[0].start){
// ret.push_back(newInterval); ret.push_back(intervals[0]);
// return ret;
// }
// if(intervals[0].end<newInterval.start){
// ret.push_back(intervals[0]); ret.push_back(newInterval);
// return ret;
// }
// }
int idx1 =binsearch(intervals,newInterval.start);
int idx2 =binsearch(intervals,newInterval.end);
int idx_tmp,newStart;
if(intervals[idx1].end>=newInterval.start){
idx_tmp = idx1-;
newStart = intervals[idx1].start<newInterval.start?intervals[idx1].start:newInterval.start;
}
else{
idx_tmp=idx1;
newStart = newInterval.start;
}
for(int i=;i<=idx_tmp;i++) ret.push_back(intervals[i]);
int newEnd;
if(newInterval.end<intervals[idx2].start){
newEnd = newInterval.end;
idx_tmp = idx2;
}
else{
newEnd = intervals[idx2].end>newInterval.end?intervals[idx2].end:newInterval.end;
idx_tmp = idx2+;
}
ret.push_back(Interval(newStart,newEnd));
for(int i=idx_tmp;i<n;i++) ret.push_back(intervals[i]);
return ret;
} int binsearch(vector<Interval> &ints,int target)
{
if(ints.size()==) return ;
int lft = ,rgt = ints.size()-;
if(ints[rgt].start<=target) return rgt;
if(ints[lft].start>=target) return lft;
int ret = ;
do{
int mid = (lft+rgt)/;
if(ints[mid].start>target) rgt=mid;
else lft=mid;
ret = lft;
if(ints[ret+].start>target) break;
}while(lft+<rgt);
return ret;
}
}; int main(){
vector<Interval> intervals{{,},{,},{,},{,},{,}};
// vector<Interval> intervals{{1,2}};
// cout<<intervals[0].start<<" "<<intervals[1].end<<endl;
Interval newInterval(-,);
Solution sol;
vector<Interval> ret = sol.insert(intervals,newInterval);
for(int i=;i<ret.size();i++)
cout<<ret[i].start<<" "<<ret[i].end<<endl;
return ;
}
[LeetCode] Insert Interval 二分搜索的更多相关文章
- 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]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 ...
- 57[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode]Insert Interval 考虑多种情况
写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)| (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
随机推荐
- 三、Linux 系统目录结构
Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最 ...
- 搭建zipkin参数配置
Environment Variables zipkin-server is a drop-in replacement for the scala query service. yaml confi ...
- ZOJ Monthly, January 2018 训练部分解题报告
A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...
- optparser 模块 提取IP,端口,用户名,密码参数模板
import optparse #class FtpClient(object): #自定义类可以自己修改 '''ftp客户端''' #def __init__(self): parser = opt ...
- easyui 判断密码是否输入一致
1.首先要扩展validatebox,添加验证两次密码功能 $.extend($.fn.validatebox.defaults.rules, { eqPassword:{ validator:fun ...
- php和js中数组的总结
php中数组的表示方法:array()或者[] js中数组的表示方法:new array()或者[] 一.php中初始化命名数组 在PHP中声明数组的方式主要有两种:一是应用array()函数声明 ...
- laravel5.2总结--redis使用
一切的前提都是已经安装好了redis服务器,并且能启动(我只总结了mac的安装方法:传送门) 我自己使用的是mac系统,有个教程可以参考下,传送门: 1.安装PHP PRedis 1>PRedi ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 生产环境下yum的配置
介绍在局域网里面配置本地yum源环境: 在私有云的服务器上配置本地yum源 在局域网中有多台服务器,网段为192.168.10.0/24在其中一台10.11配置本地yum源,其他服务器中的baseur ...