[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的其他问题 [ 做题通用的关键 ...
随机推荐
- Aliyun ECS简单的安装nginx(1.8.0)
1. yum install gcc-c++ 2. yum install -y pcre pcre-devel 3. yum install -y zlib zlib-devel 4. yum in ...
- 初级React入门
一.引入Reactjs 方法一:直接下载相关js文件引入网页,其中react.js 是 React 的核心库,react-dom.js 是提供与 DOM 相关的功能,Browser.js 的作用是将 ...
- dijkstra与他的优化!!!
目录 SPFA已死,有事烧纸 Dijkstra 配对堆 引言 讲解 合并 修改 弹出堆顶pop 代码 结合! 1 2 @ SPFA已死,有事烧纸 其实我本人也是一个SPFA的忠诚用户,至少我的最少费用 ...
- hihocoder1015 kmp算法
#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- [BZOJ2947]促销(Splay)
Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...
- Java基础知识:Collection接口
*本文是最近学习到的知识的记录以及分享,算不上原创. *参考文献见文末. 这篇文章主要讲的是java的Collection接口派生的两个子接口List和Set. 目录 Collection框架 Lis ...
- 【高精度】模板 (C++)
//n为长度 1.高精加 复杂度:O(n) #include<iostream> #include<cstring> #include<algorithm> usi ...
- 【Remove Elements】cpp
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- Java开发微信公众号(四)---微信服务器post消息体的接收及消息的处理
在前几节文章中我们讲述了微信公众号环境的搭建.如何接入微信公众平台.以及微信服务器请求消息,响应消息,事件消息以及工具处理类的封装:接下来我们重点说一下-微信服务器post消息体的接收及消息的处理,这 ...
- linux基础(基本命令)
Linux学习 1.Linux安装.配置 Linux的操作背景介绍 Linux操作系统 开源.自由且开发源代码的类Unix操作系统 厂商较多 著名的有R ...