题解 | #合并区间#
合并区间
https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a
public class Solution { public ArrayList<Interval> merge(ArrayList<Interval> intervals) { if (intervals == null || intervals.size() <= 1) { return intervals; } Collections.sort(intervals, (o1, o2) -> o1.start - o2.start); ArrayList<Interval> res = new ArrayList<>(); for (int i = 0, j; i < intervals.size(); i = j) { int L = intervals.get(i).start; int R = intervals.get(i).end; j = i + 1; for (; j < intervals.size() && R >= intervals.get(j).start; j++) { R = Math.max(R, intervals.get(j).end); } res.add(new Interval(L, R)); } return res; } }