import java.util.*; /* * public class Interval { * int start; * int end; * public Interval(int start, int end) { * this.start = start; * this.end = end; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param intervals Interval类ArrayList * @return Interval类ArrayList */ public ArrayList<Interval> merge (ArrayList<Interval> intervals) { // write code here if (intervals.size() == 0 || intervals == null) { return intervals; } intervals.sort(new Comparator<Interval>(){ public int compare(Interval o1, Interval o2){ if(o1.start != o2.start) return o1.start - o2.start; else return o1.end - o2.end; }}); for (int i = 0; i < intervals.size()-1; i++) { if (intervals.get(i).end >= intervals.get(i+1).start) { if (!(intervals.get(i).end >= intervals.get(i+1).end)) { intervals.get(i).end = intervals.get(i+1).end; } intervals.remove(i+1); i--; } } return intervals; } }