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

解题思路:

1、先用两次二分查找找到和待插入的区间有关系的区间范围;

2、将有关系的区间做处理;

3、生成新的合并后的区间并返回;

解题步骤:

代码:

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool partial_order(const Interval &a, const Interval &b) {
return a.end < b.start;
}; vector<Interval> insert(std::vector<Interval> &intervals, Interval newInterval) {
vector<Interval>::iterator less = lower_bound(intervals.begin(), intervals.end(), newInterval, partial_order);
vector<Interval>::iterator greater = upper_bound(intervals.begin(), intervals.end(), newInterval, partial_order); vector<Interval> answer;
answer.insert(answer.end(), intervals.begin(), less);
if (less < greater) {
newInterval.start = min(newInterval.start, (*less).start);
newInterval.end = max(newInterval.end, (*(greater - )).end);
}
answer.push_back(newInterval);
answer.insert(answer.end(), greater, intervals.end()); return answer;
}
};

不用lower_bound和upper_bound写的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) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
if (intervals.empty())
return vector<Interval> (, newInterval);
int left = ;
int right = intervals.size() - ;
int size = intervals.size();
int ins_start, ins_end; while (left < right) {
int mid = (left + right) / ;
if (intervals[mid].end < newInterval.start)
left = mid + ;
else
right = mid;
}
ins_start = left; left = left == ? : left - ;
right = intervals.size() - ;
while (left < right) {
int mid = (left + right) / + ;
if (newInterval.end < intervals[mid].start)
right = mid - ;
else
left = mid;
}
ins_end = right; if (ins_end == && newInterval.end < intervals[].start) {
intervals.insert(intervals.begin(), newInterval);
return intervals;
}
if (ins_start == size - && newInterval.start > intervals[size - ].end) {
intervals.push_back(newInterval);
return intervals;
} vector<Interval> answer;
answer.insert(answer.end(), intervals.begin(), intervals.begin() + ins_start);
if (ins_start <= ins_end) {
newInterval.start = min(newInterval.start, intervals[ins_start].start);
newInterval.end = max(newInterval.end, intervals[ins_end].end);
}
answer.push_back(newInterval);
answer.insert(answer.end(), intervals.begin() + ins_end + , intervals.end());
return answer; }
};

【Leetcode】【Hard】Insert Interval的更多相关文章

  1. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. 【LeetCode算法-28/35】Implement strStr()/Search Insert Position

    LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...

  7. 【LeetCode每天一题】Search Insert Position(搜索查找位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. 【leetcode刷提笔记】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. Unable to open file 'TYPES.OBJ'

    Unable to open file 'TYPES.OBJ' 有旧的控件HPP文件存在,旧控件的HPP文件里是Types::TPoint: 新的Berlin的是System::Types::TPoi ...

  2. hive 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法

    hive中一般取top n时,row_number(),rank,dense_ran()这三个函数就派上用场了, 先简单说下这三函数都是排名的,不过呢还有点细微的区别. 通过代码运行结果一看就明白了. ...

  3. 自定义底部tab

    public class MainActivity extends TabActivity implements OnCheckedChangeListener { private RadioGrou ...

  4. .NET/C#/Oracle数据库操作类

    public static class OracleHelper { //数据库连接字符串 private readonly static string connstr = Configuration ...

  5. Get the Uniqueid of Action Originate in the AMI

    [asterisk-users] Get the Uniqueid of Action Originate in the AMI Adolphe Cher-Aime acheraime at gmai ...

  6. jquery学习--属性操作

    学习jquery很长一段时间了,知道对属性操作的方式为: $("#xx1").attr("xx2"); //获取属性值 $("#xx1"). ...

  7. WiFi破解

    BT5破解WPA2-PSK无线密码实践     笔者住处附近有无线信号,觉得可以试着破解一下,于是有了如下实践. 软硬件环境:Vmware Workstation 9,BT5 r3,reaver1.4 ...

  8. AppcompatActivity闪退问题解决方案

    apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0. ...

  9. Java获取服务器网址

    StringBuffer url1 = request.getRequestURL(); String tempContextUrl1 = url1.delete(url1.length() - re ...

  10. 使用并行的方法计算斐波那契数列 (Fibonacci)

    更新:我的同事Terry告诉我有一种矩阵运算的方式计算斐波那契数列,更适于并行.他还提供了利用TBB的parallel_reduce模板计算斐波那契数列的代码(在TBB示例代码的基础上修改得来,比原始 ...