上善若水 大盈若冲
LeetCode link
solution
123456789101112131415161718192021222324252627
class Solution { public List<Interval> merge(List<Interval> intervals) { List<Interval> results = new ArrayList<>(); if (intervals.size() < 1) { return results; } Collections.sort(intervals, new Comparator<Interval>() { public int compare (Interval a, Interval b) { return a.start - b.start; } }); int start = intervals.get(0).start; int end = intervals.get(0).end; for (int i = 1; i < intervals.size(); i++) { Interval interval = intervals.get(i); if (interval.start <= end) { end = Math.max(end, interval.end); } else { results.add(new Interval(start, end)); start = interval.start; end = interval.end; } } results.add(new Interval(start, end)); return results; }}