【Leetcode】【Hard】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]
.
解题思路:
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的更多相关文章
- 【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 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【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 ...
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【leetcode刷提笔记】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【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 ...
- 【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 ...
随机推荐
- 修改Django的默认打印时间
环境 Django版本:1.10 前言 默认情况下,Django会把日期按照“月份 天数, 年”的格式打印,比如2003年2月4日会打印成“Feb. 4, 2003”,这种格式对于西方人来说很好看,但 ...
- guava cache
适用场景 缓存在很多场景下都是相当有用的.例如,计算或检索一个值的代价很高,并且对同样的输入需要不止一次获取值的时候,就应当考虑使用缓存. Guava Cache与ConcurrentMap很相似,但 ...
- delphi 10 seattle 安卓服务开发(二)
关于delphi 10 移动服务开发的几张图
- Node.js 的初步理解
Node.js 是一个采用C++语言编写的后端的 Javascript 的运行环境, 它使用了 google 的 V8虚拟机来解释和执行代码.Node.js 的有许多有用的内置的模块,比如 http, ...
- Web API - window - 获取滑动位置
这两个属性都可以获取当前页面的滑动高度 虽然取到了同样的数据,但是我相信底层还是有区别的 不然不会用浪费api吧!
- redis配置认证密码
redis配置密码1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 #requirepass foobared 去掉行前的注释,并修改 ...
- 【C#】 目前的技能点
[C#] 目前的技能点 一. C/S 1. WinForm 2. WPF 二.B/S 1. MVC 2. ASPX 3. WebService 4. js 5. jQuery , jQuery UI ...
- echo颜色显示
echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033 ...
- jQuery的动画队列
动画队列主要用到jQuery的queue.dequeue和clearqueue. 1.queue()函数主要是将一个动画函数数组绑定到一个队列上 2.dequeue()函数主要是执行队列的第一个函数, ...
- 教你理解Fragment
定义 Fragment 表示 Activity 中的行为或用户界面部分.我们可以将多个片段组合在一个 Activity 中来构建多窗口UI,以及在多个 Activity 中重复使用某个片段.可以将片段 ...