标签:
public List<Interval> merge(List<Interval> intervals) { if(intervals == null || intervals.size() < 2) return intervals; Collections.sort(intervals, new Comparator<Interval>() { @Override public int compare(Interval o1, Interval o2) { return o1.start - o2.start; } }); List<Interval> list = new ArrayList<Interval>(); list.add(intervals.get(0)); for(int i=1; i<intervals.size(); i++) { Interval current = intervals.get(i); Interval result = list.get(list.size()-1); if(current.start > result.end) { list.add(current); } else if(current.end > result.end) { list.remove(list.size()-1); list.add(new Interval(result.start, current.end)); } } return list; }
标签:
原文地址:http://www.cnblogs.com/linxiong/p/4309363.html