🔗:【贪心算法,合并区间有细节!LeetCode:56.合并区间-哔哩哔哩】
class Solution {
public:vector<vector<int>> merge(vector<vector<int>>& intervals) {if(intervals.size()==0){return intervals;}sort(intervals.begin(),intervals.end());vector<vector<int>> ans;ans.push_back(intervals[0]);for(int i = 1 ;i<intervals.size();++i){if(intervals[i][0]<=ans.back()[1]){ans.back()[1]=max(intervals[i][1],ans.back()[1]);}else{ans.push_back(intervals[i]);}}return ans;}
};