每日算法之四十: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].

这个跟之前的合并类似,最好也是逐一考虑即可,记录好newInterval即可,记得最后要插入一次,函数有两个出口,两个出口都要处理好修改后的newInterval。

/**
 * 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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
    //Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
      int size = intervals.size();
      vector<Interval> res;
      for(int i = 0; i < size; i++)
      {
          if(intervals[i].end < newInterval.start )
                res.push_back(intervals[i]);
          else if(intervals[i].start > newInterval.end)
          {
                res.push_back(newInterval);
                res.insert(res.end(),intervals.begin() + i, intervals.end() );
                return res;
          }
          else
          {
              newInterval.start = min(newInterval.start, intervals[i].start);
              newInterval.end   = max(newInterval.end, intervals[i].end);
          }
      }
      res.push_back(newInterval);
      return res;
    }
 };
    

每日算法之四十:Insert Interval,布布扣,bubuko.com

时间: 2024-10-13 19:32:55

每日算法之四十:Insert Interval的相关文章

每日算法之四十二:Permutation Sequence (顺序排列第k个序列)

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "

每日算法之四十四:Unique Path(矩阵中不重复路径的数目)

Unique Paths: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

每日算法之四十八:Plus One (数组表示的十进制加一进位)以及求Sqrt(x)

给定数组表示的十进制数,加一操作.结果依然用十进制的数组表示.这里主要注意最高位(digit[0])依然有进位,即溢出的情况. Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. <span style=

每日算法之四十六:Add Binary(二进制字符创相加)

二进制字符创相加,通过进位的方式逐位考虑.也可以把相加的过程抽象成一个函数. Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 方法一: class Solution { public: string addBinary(string a, string b) { int a_

每日算法之三十八:Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 回文字符串是指: 两个字符串的字符个数完全相同,这两个字符串是Anagrams.因此Anagrams至少指俩字符串.找出字符集合中的Anagrams组. 由此我们可以想到,只要将几个单词按照字母顺序进行排序,就可以通过比较判断他们是否是anagrams. 思路:用map

每日算法之四十三:Rotate List (列表旋转k个元素)

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这里的k可能是比链表长度要大的数字,因此实际旋转的位置就是k%len(list).如果这个计算结果等于零或者等于len(list),

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

每日算法之三十四:Multiply Strings

大数相乘,分别都是用字符串表示的两个大数,求相乘之后的结果表示. 首先我们应该考虑一下测试用例会有哪些,先准备测试用例对防御性编程会有比较大的帮助,能够考虑一些极端情况.有以下几种用例: 1)"0","0" 2)"0","879127346783" 其中一个是零 3)"as234","123343"  存在非法字符 4)"000000000000001234",&qu

每日算法之三十:Valid Sudoku (九宫格)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump