题目
注意有个类似的题目:,注意区分。
class Solution {public int[][] merge(int[][] intervals) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(intervals, (a1, a2) -> {return a1[0] - a2[0];});int left = Integer.MAX_VALUE, right = Integer.MIN_VALUE, i = 0;while (i < intervals.length) {left = Math.min(left, intervals[i][0]);right = Math.max(right, intervals[i][1]);++i;if (i == intervals.length || right < intervals[i][0]) {res.add(new ArrayList<>(Arrays.asList(left, right)));left = Integer.MAX_VALUE;right = Integer.MIN_VALUE;}}int[][] resArray = new int[res.size()][2];for (i = 0; i < res.size(); ++i) {resArray[i][0] = res.get(i).get(0);resArray[i][1] = res.get(i).get(1);}return resArray;}
}