• 力扣算法题—056合并区间


    给出一个区间的集合,请合并所有重叠的区间。

    示例 1:

    输入: [[1,3],[2,6],[8,10],[15,18]]
    输出: [[1,6],[8,10],[15,18]]
    解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
    

    示例 2:

    输入: [[1,4],[4,5]]
    输出: [[1,5]]
    解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

      1 #include "000库函数.h"
      2 
      3 
      4  struct Interval {
      5      int start;
      6      int end;
      7      Interval() : start(0), end(0) {}
      8      Interval(int s, int e) : start(s), end(e) {}
      9  };
     10 
     11  //第一印象就是排序再组合合并
     12 //失败,因为给的范围不是按顺序的
     13 //class Solution {
     14 //public:
     15 //    vector<Interval> merge(vector<Interval>& intervals) {
     16 //        if (intervals.empty())return {};
     17 //        vector<Interval>res;
     18 //        Interval p;
     19 //        p.start = intervals[0].start;
     20 //        for (int i = 1; i < intervals.size(); ++i) {
     21 //            if ((intervals[i].start - intervals[i-1].end) < 1)
     22 //                continue;
     23 //            p.end = intervals[i-1].end;
     24 //            res.push_back(p);
     25 //            p.start = intervals[i].start;
     26 //        }
     27 //        p.end = intervals[intervals.size()-1].end;
     28 //        res.push_back(p);
     29 //        return res;
     30 //    }
     31 //};
     32 
     33 //仍然是先排序在合并的思想
     34 //但是改进了,将头尾分开排序
     35 //20ms
     36  class Solution {
     37  public:
     38      vector<Interval> merge(vector<Interval>& intervals) {
     39          if (intervals.empty())return {};
     40          vector<Interval>res;
     41          vector<int>s, e;
     42          for (int i = 0; i < intervals.size(); ++i) {
     43              s.push_back(intervals[i].start);
     44              e.push_back(intervals[i].end);
     45          }
     46          sort(s.begin(), s.end());//头排序
     47          sort(e.begin(), e.end());//尾排序
     48          Interval p;
     49          p.start = s[0];
     50          for (int i = 1; i < s.size(); ++i) {
     51              if ((s[i] - e[i - 1]) < 1)continue;//比较大小
     52              p.end = e[i - 1];
     53              res.push_back(p);
     54              p.start = s[i];
     55          }
     56          p.end = e[s.size() - 1];
     57          res.push_back(p);
     58          return res;
     59      }
     60  };
     61 
     62  //将上述方法改进,使用自定义排序函数
     63  class Solution {
     64  public:
     65      vector<Interval> merge(vector<Interval>& intervals) {
     66          if (intervals.empty()) return {};
     67          sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {return a.start < b.start; });
     68          vector<Interval> res{ intervals[0] };
     69          for (int i = 1; i < intervals.size(); ++i) {
     70              if (res.back().end < intervals[i].start) {
     71                  res.push_back(intervals[i]);
     72              }
     73              else {
     74                  res.back().end = max(res.back().end, intervals[i].end);
     75              }
     76          }
     77          return res;
     78      }
     79  };
     80 
     81 
     82 //这道题还有另一种解法,由于插入的过程中也有合并的操作,所以我们可以建立一个空的集合,
     83 //然后把区间集的每一个区间当做一个新的区间插入结果中,也可以得到合并后的结果,
     84 
     85  class Solution {
     86  public:
     87      vector<Interval> merge(vector<Interval>& intervals) {
     88          vector<Interval> res;
     89          for (int i = 0; i < intervals.size(); ++i) {
     90              res = insert(res, intervals[i]);
     91          }
     92          return res;
     93      }
     94      vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
     95          vector<Interval> res;
     96          int n = intervals.size(), cur = 0;
     97          for (int i = 0; i < n; ++i) {
     98              if (intervals[i].end < newInterval.start) {
     99                  res.push_back(intervals[i]);
    100                  ++cur;
    101              }
    102              else if (intervals[i].start > newInterval.end) {
    103                  res.push_back(intervals[i]);
    104              }
    105              else {
    106                  newInterval.start = min(newInterval.start, intervals[i].start);
    107                  newInterval.end = max(newInterval.end, intervals[i].end);
    108              }
    109          }
    110          res.insert(res.begin() + cur, newInterval);
    111          return res;
    112      }
    113  };
    114 
    115 
    116 
    117 void T056() {
    118     Solution s;
    119     vector<Interval>v;
    120     vector<int>nums;
    121     Interval p;
    122     nums = { 1,3,2,6,8,10,15,18 };
    123     for (int i = 0; i < nums.size(); i += 2) {
    124         p.start = nums[i];
    125         p.end = nums[i + 1];
    126         v.push_back(p);
    127     }
    128     v = s.merge(v);
    129     for (auto a : v)
    130         cout << a.start << ", " << a.end << ";  ";
    131     cout << endl;
    132 
    133     v.resize(0);
    134     nums = { 1,4,4,5 };
    135     for (int i = 0; i < nums.size(); i += 2) {
    136         p.start = nums[i];
    137         p.end = nums[i + 1];
    138         v.push_back(p);
    139     }
    140     v = s.merge(v);
    141     for (auto a : v)
    142         cout << a.start << ", " << a.end << ";  ";
    143     cout << endl;
    144 
    145     v.resize(0);
    146     nums = { 1,4,0,4 };
    147     for (int i = 0; i < nums.size(); i += 2) {
    148         p.start = nums[i];
    149         p.end = nums[i + 1];
    150         v.push_back(p);
    151     }
    152     v = s.merge(v);
    153     for (auto a : v)
    154         cout << a.start << ", " << a.end << ";  ";
    155     cout << endl;
    156 
    157 }
  • 相关阅读:
    client offset screen 的区别
    js中const,var,let区别
    jquery的选择器
    gulp
    JS 实现图片放大效果
    html单个标签实现跑马灯效果
    前端之HTML知识点整理
    各种纯css图标
    防止反复点击的思路
    .NET Memcached Client 扩展获取所有缓存Key
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10637307.html
Copyright © 2020-2023  润新知