https://leetcode.cn/problems/merge-intervals/?envType=study-plan-v2&envId=top-100-liked
合并区间练习题
typedef pair<int,int> PII;
vector<PII> segs;
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
segs.clear();
for(int i=0;i<intervals.size();i++)
{
int l=intervals[i][0],r=intervals[i][1];
segs.push_back({l,r});
}
merge(segs);
vector<vector<int>> res;
for(int i=0;i<segs.size();i++)
{
vector<int> temp;
temp.clear();
temp.push_back(segs[i].first);
temp.push_back(segs[i].second);
res.push_back(temp);
}
return res;
}
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
for (auto seg : segs)
if (ed < seg.first)
{
if (st != -2e9) res.push_back({st, ed});
st = seg.first, ed = seg.second;
}
else ed = max(ed, seg.second);
if (st != -2e9) res.push_back({st, ed});
segs = res;
}
};
标签:ed,56,合并,back,segs,st,seg,res,leetcode From: https://www.cnblogs.com/lxl-233/p/18157257