986. 区间列表的交集 题解

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>> ans;
        int i=0,j=0;
        int left=0,right=0;
        while(i<firstList.size()&&j<secondList.size()){
            left=max(firstList[i][0],secondList[j][0]);
            right=min(firstList[i][1],secondList[j][1]);
            if(left<=right){
                ans.push_back({left,right});
            }
            if(firstList[i][1]<secondList[j][1]){
                i++;
            }else{
                j++;
            }
        }
        return ans;
    } 
};

 

posted @ 2022-03-25 19:41  shiningrise  阅读(20)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css