合并区间(双指针)

合并区间(双指针)_第1张图片

class Solution {
     
public:
	vector<vector<int>> merge(vector<vector<int>>& intervals) {
     
		sort(intervals.begin(), intervals.end());
		vector<vector<int>> res;
		int i = 0;
		while (i < intervals.size())
		{
     
			int left = intervals[i][0], right = intervals[i][1];
			while (i + 1 < intervals.size() && intervals[i + 1][0] <= right)
			{
     
				right = max(right, intervals[i + 1][1]);
				i++;
			}
			res.push_back({
      left,right });

			i++;
		}
		return res;
	}
};

你可能感兴趣的:(合并区间(双指针))