File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -37,3 +37,50 @@ public:
37
37
return result;
38
38
}
39
39
};
40
+
41
+ class Solution {
42
+ public:
43
+ static bool myfunction(const Interval a,const Interval b) // 一定要加static,,否则会报错
44
+ {
45
+ return a.start < b.start;
46
+ }
47
+ vector<Interval> merge(vector<Interval> &intervals) {
48
+ vector<Interval> result;
49
+ if(intervals.size() > 1)
50
+ {
51
+ sort(intervals.begin(),intervals.end(),myfunction);
52
+ bool flag = false; // 标记是否是连续合并的
53
+ int tempMax = intervals[0].end;
54
+ for(int i = 1;i < intervals.size();i++)
55
+ {
56
+ if(tempMax < intervals[i].start)
57
+ {
58
+ if(!flag)
59
+ result.push_back(intervals[i-1]);
60
+ if(i == intervals.size()-1)
61
+ result.push_back(intervals[i]);
62
+ flag = false;
63
+ }
64
+ else
65
+ {
66
+
67
+ if(flag) // 连续是可合并,看看能否合并成一个
68
+ {
69
+ result.back().end = max(tempMax,intervals[i].end);
70
+ tempMax = result.back().end;
71
+ continue;
72
+ } // 这里感觉可以优化。。
73
+ Interval *newVal = new Interval(intervals[i-1].start,max(intervals[i-1].end,intervals[i].end));
74
+ result.push_back(*newVal);
75
+ flag = true;
76
+ }
77
+ tempMax = max(intervals[i-1].end,intervals[i].end);
78
+ }
79
+ }
80
+ else
81
+ result = intervals;
82
+ return result;
83
+ }
84
+ };
85
+
86
+
You can’t perform that action at this time.
0 commit comments