• [容易]插入区间


    题目来源:http://www.lintcode.com/zh-cn/problem/insert-interval/

    可以accept的程序如下:

     1 /**
     2  * Definition of Interval:
     3  * class Interval {
     4  * public:
     5  *     int start, end;
     6  *     Interval(int start, int end) {
     7  *         this->start = start;
     8  *         this->end = end;
     9  *     }
    10  * }
    11  */
    12 bool comp(Interval a, Interval b) {
    13     return a.start < b.start;
    14 }
    15 
    16 class Solution {
    17 public:
    18     /**
    19      * Insert newInterval into intervals.
    20      * @param intervals: Sorted interval list.
    21      * @param newInterval: new interval.
    22      * @return: A new interval list.
    23      */
    24     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
    25         // write your code here
    26             intervals.insert(intervals.end(), newInterval);
    27             sort(intervals.begin(), intervals.end(), comp);
    28             int len = intervals.size();
    29             vector<Interval> res;
    30             int i;
    31             for (i = 1; i < len; ++i) {
    32                 while (intervals[i].start <= intervals[i-1].end && i < len) {
    33                     intervals[i].start = intervals[i-1].start;
    34                     intervals[i].end = intervals[i].end > intervals[i-1].end ?
    35                     intervals[i].end : intervals[i-1].end;
    36                     i++;
    37                 }
    38                 res.push_back(intervals[i-1]);
    39                 if (i >= len) {
    40                     return res;
    41                 }
    42             }
    43             res.push_back(intervals[i-1]);
    44             return res;
    45     }
    46 };

    完整的测试程序如下:

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 class Interval {
     8 public:
     9     int start, end;
    10     Interval(int start, int end) {
    11         this->start = start;
    12         this->end = end;
    13     }
    14 };
    15 
    16 bool comp(Interval a, Interval b) {
    17     return a.start < b.start;
    18 }
    19 
    20 class Solution {
    21 public:
    22     /**
    23      * Insert newInterval into intervals.
    24      * @param intervals: Sorted interval list.
    25      * @param newInterval: new interval.
    26      * @return: A new interval list.
    27      */
    28     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
    29             intervals.insert(intervals.end(), newInterval);
    30             sort(intervals.begin(), intervals.end(), comp);
    31             int len = intervals.size();
    32             vector<Interval> res;
    33             int i;
    34             for (i = 1; i < len; ++i) {
    35                 while (i < len && intervals[i].start <= intervals[i-1].end) {
    36                     intervals[i].start = intervals[i-1].start;
    37                     intervals[i].end = intervals[i].end > intervals[i-1].end ?
    38                     intervals[i].end : intervals[i-1].end;
    39                     i++;
    40                 }
    41                 res.push_back(intervals[i-1]);
    42                 if (i >= len) {
    43                     return res;
    44                 }
    45             }
    46             res.push_back(intervals[i-1]);
    47             return res;
    48     }
    49 };
    50 
    51 int main()
    52 {
    53     Solution solu;
    54     Interval i(1,2);
    55     Interval j(5,9);
    56     //Interval k(3,4);//[1,2],[3,4],[5,9]
    57     //Interval k(2,5);//[1,9]
    58     //Interval k(2,4);//[1,4],[5,9]
    59     //Interval k(3,5);//[1,2],[3,9]
    60     Interval k(0,4);//[0,4],[5,9]
    61     vector<Interval> a;
    62     vector<Interval> b;
    63     a.push_back(i);
    64     a.push_back(j);
    65     b=solu.insert(a,k);
    66     for(int i=0;i<b.size();i++)
    67         cout<<b[i].start<<","<<b[i].end<<endl;
    68 }
  • 相关阅读:
    浏览器缓存
    FLINK --- 写HDFS
    ArrayList 线程不安全
    rocketMQ源码之 似乎并不能严格进行顺序消费
    报错 Aray size is not a small enough positive integer 的解决方案
    什么是函数的柯里化?
    props, state与render函数关系 – 数据和页面是如何实现互相联动的?
    【自我管理】my schedule
    【解题报告】洛谷P1219 八皇后
    【解题报告】洛谷P1433 吃奶酪
  • 原文地址:https://www.cnblogs.com/hslzju/p/5456489.html
Copyright © 2020-2023  润新知