LintCode #30. Insert Interval (Easy)

LeetCode #57. Insert Interval (Hard)

  1. class Solution {
  2. public:
  3. vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
  4. vector<Interval> v;
  5. auto it = intervals.begin();
  6. while (it != intervals.end() && it->end < newInterval.start) {
  7. v.push_back(*it);
  8. ++it;
  9. }
  10. auto left = it;
  11. while (it != intervals.end() && it->start <= newInterval.end) ++it;
  12. auto right = it;
  13. if (right - left != 0) {
  14. v.push_back(Interval(min(left->start, newInterval.start), max((right - 1)->end, newInterval.end)));
  15. } else {
  16. v.push_back(newInterval);
  17. }
  18. while (it != intervals.end()) {
  19. v.push_back(*it);
  20. ++it;
  21. }
  22. return v;
  23. }
  24. };

思路:

  • 第一个while循环跳过所有不会与newInterval相交且小于newInterval的区间.
  • 中间一段计算相交区间.
  • 最后一个while循环跳过所有不会与newInterval相交且大于newInterval的区间.

时间复杂度: O(n)

空间复杂度: O(n)


参照九章算法的JAVA解法改写的C++版.

  1. class Solution {
  2. public:
  3. vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
  4. vector<Interval> v;
  5. int pos = 0;
  6. for (auto interval : intervals) {
  7. if (interval.end < newInterval.start) {
  8. v.push_back(interval);
  9. ++pos;
  10. } else if (interval.start > newInterval.end) {
  11. v.push_back(interval);
  12. } else {
  13. newInterval.start = min(newInterval.start, interval.start);
  14. newInterval.end = max(newInterval.end, interval.end);
  15. }
  16. }
  17. v.insert(v.begin() + pos, newInterval);
  18. return v;
  19. }
  20. };

这算法简洁在于: 一个循环, 通过两个if滤掉所有不相交的区间; (若有)剩下的区间一定与newInterval相交, 将这些区间合并到newInterval里, 最后插入即可.

唯一欠缺的地方在于插入操作会有额外的时间开销.

[OJ] Insert Interval的更多相关文章

  1. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  4. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  5. [Swift]LeetCode57. 插入区间 | Insert Interval

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

  6. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  9. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

随机推荐

  1. ActionBar只显示图标不显示文字

    问题:ActionBar菜单项android:showAsAction设置为android:showAsAction="always|withText"或者android:show ...

  2. ios PromiseKit

    简介: 高级开发是高度异步的,PromiseKit收集了一些帮助函数,让我们开发过程中使用的典型异步模式更加令人愉悦. 1.通过pod安装promisekit: 2. promise.h介绍 @imp ...

  3. IAP (In-App Purchase)中文文档

    内容转自:http://yarin.blog.51cto.com/1130898/549141 一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程 ...

  4. Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB

    Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB ...

  5. requirejs下载与配置

    写在开始: requirejs有中文版api文档,可以访问http://www.requirejs.cn/home.html 下载requirejs 访问http://www.requirejs.cn ...

  6. java线程中生产者与消费者的问题

    一.概念 生产者与消费者问题是一个金典的多线程协作的问题.生产者负责生产产品,并将产品存放到仓库:消费者从仓库中获取产品并消费.当仓库满时,生产者必须停止生产,直到仓库有位置存放产品:当仓库空时,消费 ...

  7. 05_Excel操作_02_模拟Web环境的User列表导出

    [思路解释] 在正式上到WebProject之前,准备模拟一下WebProject后台的导出流程. 主要都写在ExcelService层,在Excel的Service层,首先要获得UserList,即 ...

  8. python 访问php程序,实现定时

    #!/usr/bin/python #test2.py import sys import urllib2 j = True jj = 1##########用于统计,所以分页, url = 'htt ...

  9. PHP类中的__get()和__set函数到底有什么用?

    当试图获取一个不可达变量时,类会自动调用__get. 同样的,当试图设置一个不可达变量时,类会自动调用__set. 在网站中,这两个并不是什么非用不可的函数.   例如: Class Test {   ...

  10. SSH框架jar神包

    SSH JAR包链接(https://zhidao.baidu.com/share/ac8b1389bac84023d7442cad88f3933c.html)