-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path57.插入区间.cpp
42 lines (40 loc) · 1.1 KB
/
57.插入区间.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode.cn id=57 lang=cpp
*
* [57] 插入区间
*/
// @lc code=start
class Solution {
public:
static bool cmp(const vector<int>& vec1,const vector<int>& vec2){
return vec1[0] < vec2[0];
}
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int left = newInterval[0];
int right = newInterval[1];
vector<vector<int>> ans;
bool flag = false;
for(const auto& interval: intervals){
if(interval[1] < left){
ans.push_back(interval);
}
else if(interval[0] > right){
if(!flag){
ans.push_back({left,right});
flag = true;
}
ans.push_back(interval);
}else{
left = min(left,interval[0]);
right = max(right,interval[1]);
}
}
if(!flag){
ans.push_back({left,right});
}
//ans.push_back({left,right});
//sort(ans.begin(),ans.end(),cmp);
return ans;
}
};
// @lc code=end