- 浏览: 89183 次
- 性别:
- 来自: 南京
最新评论
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]
.
方法:高端方法,线段树!不过我忘了怎么写~~因此写点简单的方法。
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ #include <algorithm> using namespace std; int cmp(const Interval& a, const Interval& b) {return a.end < b.end;} int cmp2(const Interval& a, const Interval& b) {return a.start < b.start;} class Solution { public: vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { vector<Interval>::iterator it; it = lower_bound(intervals.begin(), intervals.end(), Interval(0, newInterval.start), cmp); if (it == intervals.end()) { vector<Interval> res(intervals.begin(), intervals.end()); res.push_back(newInterval); sort(res.begin(), res.end(), cmp2); return res; } else { // newInterval.start <= it.end if (it->start > newInterval.end) { vector<Interval> res(intervals.begin(), intervals.end()); res.push_back(newInterval); sort(res.begin(), res.end(), cmp2); return res; } else { vector<Interval> res(intervals.begin(), it); vector<Interval>::iterator last = lower_bound(it, intervals.end(), newInterval, cmp); if (last == intervals.end()) { res.push_back( Interval(min(it->start, newInterval.start), newInterval.end)); sort(res.begin(), res.end(), cmp2); return res; } else { if (last->start > newInterval.end) res.push_back(Interval(min(it->start, newInterval.start), newInterval.end)), last--; else res.push_back(Interval(min(it->start, newInterval.start), max(last->end, newInterval.end))); while (++last != intervals.end()) res.push_back(*last); sort(res.begin(), res.end(), cmp2); return res; } } } } };
发表评论
-
[Leetcode] Gas Station
2013-10-07 00:02 925Gas Station AC Rate: 101 ... -
[Leetcode] Word Break 1 & 2
2013-10-05 23:26 1372Word Break AC Rate: 2/1 ... -
[Leetcode] Single Number 1 & 2
2013-10-05 22:50 2759Single Number AC Rat ... -
[Leetcode] Copy List with Random Pointer
2013-10-05 22:43 578Copy List with Random Pointe ... -
[Leetcode] Path Sum
2013-10-03 23:52 592Path Sum AC Rate: 87 ... -
[Leetcode] Remove Duplicates from Sorted List 1 & 2
2013-10-03 23:34 975Remove Duplicates from Sort ... -
[Leetcode] Scramble String
2013-10-03 16:55 1836Scramble String AC Rate ... -
[Leetcode] Partition List
2013-09-29 23:38 663Partition List AC Rate: ... -
[Leetcode] Word Search
2013-09-21 23:21 855Given a 2D board and ... -
[Leetcode] Decode Ways
2013-09-11 17:33 682Decode WaysJun 25 '126747 / 2 ... -
[Leetcode] Convert Sorted List to Binary Search Tree
2013-09-11 16:06 714Convert Sorted List to Binary ... -
[Leetcode] Binary Tree Level Order Traversal
2013-09-10 14:39 783Binary Tree Level Order Trave ... -
[Leetcode] Subsets 1 ^& 2
2013-09-09 21:42 870SubsetsApr 18 '126226 / 1626 ... -
[Leetcode] Unique Paths II
2013-09-09 21:10 762Unique Paths IIMar 29 '124573 ... -
[Leetcode] Flatten Binary Tree to Linked List
2013-09-09 02:35 728Flatten Binary Tree to Linked ... -
[Leetcode] Triangle
2013-09-04 16:32 929TriangleOct 30 '126503 / 1779 ... -
[Leetcode] Balanced Binary Tree
2013-08-27 20:53 609Balanced Binary TreeOct 9 '12 ... -
[Leetcode] Rotate Image
2013-08-22 15:54 977Rotate ImageMar 18 '124182 / ... -
[Leetcode] Permutations / Permutations II
2013-08-22 15:41 2507Permutations IIMar 17 '12494 ... -
[Leetcode] Jump Game / Jump Game II
2013-08-22 10:31 663Jump GameMar 25 '126923 / 162 ...
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
大佬的leetcode刷题笔记(c++版本)
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题
LeetCode 刷题笔记
(C++)LeetCode刷题题解答案
LeetCode 刷题
leetcode全套解答python版本。包括更新到10月份的的leetcode
这个是leetcode发布的ebook 包含有常见题目的解答以及讲解