• 力扣算法题—057插入区间


    给出一个无重叠的 ,按照区间起始端点排序的区间列表。

    在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

    示例 1:

    输入: intervals = [[1,3],[6,9]], newInterval = [2,5]
    输出: [[1,5],[6,9]]
    

    示例 2:

    输入: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
    输出: [[1,2],[3,10],[12,16]]
    解释: 这是因为新的区间 [4,8][3,5],[6,7],[8,10] 重叠。

      1 #include "000库函数.h"
      2 
      3 
      4 
      5 struct Interval {
      6     int start;
      7     int end;
      8     Interval() : start(0), end(0) {}
      9     Interval(int s, int e) : start(s), end(e) {}
     10 };
     11  
     12 //与上题合并区间没什么区别
     13 //只不过将区间加入进去而已
     14 
     15 class Solution {
     16 public:
     17     vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
     18         if (intervals.empty())return { newInterval };
     19         vector<Interval>res;
     20         vector<int>s, e;
     21         for (int i = 0; i < intervals.size(); ++i) {
     22             s.push_back(intervals[i].start);
     23             e.push_back(intervals[i].end);
     24         }
     25         s.push_back(newInterval.start);
     26         e.push_back(newInterval.end);
     27         sort(s.begin(), s.end());//头排序
     28         sort(e.begin(), e.end());//尾排序
     29         Interval p;
     30         p.start = s[0];
     31         for (int i = 1; i < s.size(); ++i) {
     32             if ((s[i] - e[i - 1]) < 1)continue;//比较大小
     33             p.end = e[i - 1];
     34             res.push_back(p);
     35             p.start = s[i];
     36         }
     37         p.end = e[s.size() - 1];
     38         res.push_back(p);
     39         return res;
     40     }
     41 };
     42 
     43 
     44 //题目中确保原来的区间是按起始端且无重叠,则复杂度较上题更小
     45 class Solution {
     46 public:
     47     vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
     48         vector<Interval> res;
     49         int n = intervals.size(), cur = 0;
     50         while (cur < n && intervals[cur].end < newInterval.start) {
     51             res.push_back(intervals[cur++]);
     52         }
     53         while (cur < n && intervals[cur].start <= newInterval.end) {
     54             newInterval.start = min(newInterval.start, intervals[cur].start);
     55             newInterval.end = max(newInterval.end, intervals[cur].end);
     56             ++cur;
     57         }
     58         res.push_back(newInterval);
     59         while (cur < n) {
     60             res.push_back(intervals[cur++]);
     61         }
     62         return res;
     63     }
     64 };
     65 
     66 
     67 
     68 
     69 
     70 
     71 void T057() {
     72     Solution s;
     73     vector<Interval>v;
     74     Interval newInterval;
     75     vector<int>nums;
     76     Interval p;
     77     nums = { 1,4,8,10 };
     78     for (int i = 0; i < nums.size(); i += 2) {
     79         p.start = nums[i];
     80         p.end = nums[i + 1];
     81         v.push_back(p);
     82     }
     83     newInterval.start = 5;
     84     newInterval.end = 7;
     85     v = s.insert(v, newInterval);
     86     for (auto a : v)
     87         cout << a.start << ", " << a.end << ";  ";
     88     cout << endl;
     89 
     90     v.clear();
     91     nums = { 1,4,8,10 };
     92     for (int i = 0; i < nums.size(); i += 2) {
     93         p.start = nums[i];
     94         p.end = nums[i + 1];
     95         v.push_back(p);
     96     }
     97     newInterval.start = 3;
     98     newInterval.end = 5;
     99     v = s.insert(v, newInterval);
    100     for (auto a : v)
    101         cout << a.start << ", " << a.end << ";  ";
    102     cout << endl;
    103 
    104     v.clear();
    105     nums = { 1,4,8,10 };
    106     for (int i = 0; i < nums.size(); i += 2) {
    107         p.start = nums[i];
    108         p.end = nums[i + 1];
    109         v.push_back(p);
    110     }
    111     newInterval.start = 0;
    112     newInterval.end = 7;
    113     v = s.insert(v, newInterval);
    114     for (auto a : v)
    115         cout << a.start << ", " << a.end << ";  ";
    116     cout << endl;
    117 
    118     v.clear();
    119     nums = { 1,4,8,10 };
    120     for (int i = 0; i < nums.size(); i += 2) {
    121         p.start = nums[i];
    122         p.end = nums[i + 1];
    123         v.push_back(p);
    124     }
    125     newInterval.start = 2;
    126     newInterval.end = 9;
    127     v = s.insert(v, newInterval);
    128     for (auto a : v)
    129         cout << a.start << ", " << a.end << ";  ";
    130     cout << endl;
    131 
    132     
    133 }
  • 相关阅读:
    北京华思世纪思科CCNA中文视频教程全集(迅雷)
    [转载]告诉你C盘里的每个文件夹都是干什么用的(转载)
    各种路由器默认用户名及密码
    思科路由器交换机单臂路由连接方式
    大学计算机类教学视屏
    如何看待目前的路由交换机安全性
    跨越交换机的TPLink交换机配置VLAN部分图解(二)
    跨越交换机的TPLink交换机配置VLAN部分图解(一)
    基于华为路由器接入的解决方案
    跨越交换机的TPLink交换机配置VLAN部分图解(三)
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10649406.html
Copyright © 2020-2023  润新知