Leetcode 986. Interval List Intersections 区间交集 双指针

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
        int i=0, j=0;
        vector<vector<int>> res;
        while(i<A.size() && j<B.size()){
            int a1 = A[i][0], a2 = A[i][1];
            int b1 = B[j][0], b2 = B[j][1];
            
            if(b2>=a1 && b1<=a2){
                res.push_back({max(a1,b1), min(a2, b2)});
            }
            if(b2>a2) i++;
            else j++;
            
        }
        return res;
    }
};
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页