``````class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),comp);
vector<vector<int>> res;
int size = intervals.size();
if(size == 1)
return vector<vector<int>>{{intervals[0]}};

vector<int> x = intervals[0];
for(int i = 1;i < size;i++)
{
if(x[1] < intervals[i][0])
{
res.push_back({x[0],intervals[i - 1][1]});
x = intervals[i];
}

if(i == size - 1)
res.push_back({x[0],intervals[i][1]});
}

return res;
}
private:
static bool comp(const vector<int>& a,const vector<int>& b) {
return a[1] < b[1];
}
};``````

``````class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
int size = intervals.size();
if (size <= 0) return res;
sort(intervals.begin(),intervals.end(),comp);
res.push_back(intervals[0]);

for(int i = 1;i < intervals.size();i++)
{
vector<int>& x = res.back();
if(intervals[i][0] > x[1])
res.push_back(intervals[i]);
else
x[1] = max(x[1],intervals[i][1]);
}

return res;
}
private:
static bool comp(const vector<int>& a,const vector<int>& b) {
return a[0] < b[0];
}
};``````