1 /************************************** * ************************** 2 created: filename: merge-intervals.cpp 4 Author: justme0 (http://blog.csdn.net/justme0) 5 6 purpose: merge crossover intervals 7 https://oj.leetcode.com/problems/merge-intervals/ 8 ********************************* * *****************************/9 10 # DEFINE _ crt_secure_no_warnings 11 # include <iostream> 12 # include <vector> 13 # include <algorithm> 14 # include <cassert> 15 using namespace STD; 16 17 struct interval {18 int start; 19 int end; 20 21 interval (): start (0), end (0) {}22 interval (int s, int e): start (s), end (e) {}23 }; 24 25/** 26 * definition for an interval. 27 * struct interval {28 * int start; 29 * int end; 30 * interval (): Start (0), end (0) {} 31 * interval (INT s, int e): start (s), end (e) {} 32 *}; 33 */34 class solution {35 public: 36 vector <interval> Merge (vector <interval> & intervals) {37 If (intervals. empty () {38 return vector <interval> (); 39} 40 41 vector <point> coordinates; 42 for (Auto iter = intervals. begin (); iter! = Intervals. end (); ++ ITER) {43 coordinates. push_back (point (ITER-> Start, true); 44 coordinates. push_back (point (ITER-> end, false); 45} 46 sort (coordinates. begin (), coordinates. end (), CMP); 47 48 int depth = 0; // overwrite the depth of 49 assert (coordinates. front (). isleft); 50 vector <interval> merged_itvs; 51 int start = 0; 52 for (Auto iter = coordinates. begin (); iter! = Coordinates. end (); ++ ITER) {53 assert (depth> = 0); 54 if (0 = depth) {55 start = ITER-> X; // at this time, the stack is empty. ITER points to the left endpoint and starts to merge a 56} 57 ITER-> isleft? + Depth: -- depth; 58 If (0 = depth) {59 merged_itvs.push_back (interval (START, ITER-> X); // "Stack empty" at this time ", ITER points to the right endpoint and identifies a 60} 61} 62 63 return merged_itvs; // The interface is poorly designed 64} 65 66 struct point {67 int X; 68 bool isleft; 69 70 point (): x (0), isleft (false) {} 71 72 point (INT _ x, bool _ isleft): X (_ x ), isleft (_ isleft) {} 73 74 // bool operator <(const point & Other) const {75 // If (this-> x <Other. X) {76 // return true; 77 //} else if (this-> x> Other. x) {78 // return false; 79 //} else if (this-> isleft = Other. isleft) {80 // return this <& Other; 81 //} else {82 // return this-> isleft; 83 //} 84 //} 85 }; 86 87 static bool CMP (const point & A, const point & B) {88 if (. x <B. x) {89 return true; 90} else if (. x> B. x) {91 return false; 92} else if (. isleft = B. isleft) {93 R Eturn false; 94} else {95 return. isleft; 96} 97} 98 99}; 100 101 int main (INT argc, char ** argv) {102 freopen ("cin.txt", "r", stdin ); 103 104 vector <interval> VEC; 105 int A, B; 106 While (CIN> A> B) {107 Vec. push_back (interval (A, B); 108} 109 vector <interval> ans = solution (). merge (VEC); 111 for (Auto ite = ans. begin (); ite! = Ans. end (); ++ ITE) {112 cout <ite-> Start <''<ite-> end <Endl; 113} 114 115 system ("pause"); 116 return 0; 117}
Leetcode merge intervals