interval intersection
class Interval{ int start; int end; } public List<Integer> intersection(List<Interval> l1, List<Interval> l2){ List<Interval> res = new ArrayList<>(); int i = 0; int j = 0; while(i < l1.length && j < l2.length){ if(l1[i].end <= l2[j].end){ if(l1[i].end > l2[j].start){ res.add(new Interval(Math.max(l1[i].start, l2[j].start), l1[i].end)); } i++; }else{ if(l2[j].end > l1[i].start){ res.add(new Interval(Math.max(l1[i].start, l2[j].start), l2[j].end)); } j++; } } return res; }
example: input is two non-overlapping list of intervals , output is the list of overlaps
l1 : [0, 2], [5, 7]. [9, 11]
l2: [1, 3], [6, 10]
overlap is [1, 2]. [6, 7], [9, 10]
posted on 2018-08-11 03:23 猪猪🐷 阅读(240) 评论(0) 收藏 举报
浙公网安备 33010602011771号