题目链接:https://leetcode-cn.com/problems/interval-list-intersections/
题目描述:
题解:
class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
vector<vector<int>> result;
if(firstList.size() == 0 || secondList.size() == 0)
return result;
int i = 0;
int j = 0;
while(i < firstList.size() && j < secondList.size())
{
int left = max(firstList[i][0], secondList[j][0]); //子区间左边界
int right = min(firstList[i][1], secondList[j][1]); //子区间右边界
if(left <= right)
result.push_back({left, right});
firstList[i][1] > secondList[j][1] ? j++ : i++; //哪一个区间先结束,指针就先移动
}
return result;
}
};