Java实现LeetCode #986


class Solution {
public:
    vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
        vector<Interval> result;
        int i=0;
        int j=0;
        while(i<A.size()&&j<B.size()) // 用两个指针遍历,计算相交的区间
        {
            int start=max(A[i].start,B[j].start);
            int end=min(A[i].end,B[j].end);
            if(start<=end) result.push_back({start,end});
            if(A[i].end<B[j].end) i++; // 根据终点的大小,决定移动哪一个指针
            else j++;
        }
        return result;
    }
};
 
原文地址:https://www.cnblogs.com/a1439775520/p/12947023.html