美文网首页facebook 面经
FB 电面 寻找两个区间表的交集

FB 电面 寻找两个区间表的交集

作者: Anseis | 来源:发表于2018-09-14 10:50 被阅读0次

双指针

public List<Interval> commonOnlineInterval(List<Interval> a, List<Interval> b){
        int i = 0, j =0;
        List<Interval> res = new ArrayList<Interval>();
        while(i<a.size()&&j<b.size()){
            if(a.get(i).start >= b.get(j).end)j++;
            else if(b.get(j).start >= a.get(i).end) i++;
            else if(a.get(i).start < b.get(j).end){
            res.add(new Interval(Math.max(a.get(i).start,b.get(j).start),Math.min(a.get(i).end,b.get(j).end)));
            j++;
            }
            else if(b.get(j).start < a.get(i).end){
            res.add(new Interval(Math.max(a.get(i).start,b.get(j).start),Math.min(a.get(i).end,b.get(j).end)));
            i++;
            }
        }
        return res;
    }

相关文章

网友评论

    本文标题:FB 电面 寻找两个区间表的交集

    本文链接:https://www.haomeiwen.com/subject/abangftx.html