Leetcode 56 合并区间
朴素解法,逻辑的渐进。
JAVA:
public final int[][] merge(int[][] intervals) { sort(intervals); Stack<int[]> reStack = new Stack<int[]>(); for (int i = 0; i < intervals.length; i++) { if (reStack.size() == 0) { reStack.push(intervals[i]); continue; } int[] pre = reStack.get(reStack.size() - 1); int[] curr = intervals[i]; if (curr[0] > pre[1]) { reStack.push(curr); continue; } if (curr[1] > pre[1]) { pre[1] = curr[1]; } } return stackToArr(reStack); } private final int[][] stackToArr(Stack<int[]> stack) { int[][] arr = new int[stack.size()][2]; for (int i = stack.size() - 1; i >= 0; i--) { arr[i] = stack.pop(); } return arr; } private final void sort(int[][] source) { for (int i = 0; i < source.length; i++) { for (int j = i + 1; j < source.length; j++) { if (source[j][0] < source[i][0]) { int[] temp = source[i]; source[i] = source[j]; source[j] = temp; } } } }
JAVA 解法优化写法:
public final int[][] merge2(int[][] intervals) { Arrays.sort(intervals, (num1, num2) -> num1[0] - num2[0]); int len = intervals.length; int[][] reArr = new int[len][2]; int point = -1; for (int i = 0; i < intervals.length; i++) { if (point == -1 || intervals[i][0] > reArr[point][1]) { reArr[++point] = intervals[i]; continue; } reArr[point][1] = Math.max(intervals[i][1], reArr[point][1]); } return Arrays.copyOf(reArr, point + 1); }
JS 解法:
var merge = function (intervals) {
intervals.sort((v1,v2)=>v1[0]-v2[0]);
let reArr =[];
let len = intervals.length;
let point = -1;
for (let i = 0; i < len; i++) {
if (point == -1) {
reArr.push(intervals[i]);
} else if (intervals[i][0] > reArr[point][1]) {
reArr.push(intervals[i]);
} else {
reArr[point][1] = intervals[i][1] > reArr[point][1] ? intervals[i][1] : reArr[point][1];
point--;
}
point++;
}
return reArr;
};
当你看清人们的真相,于是你知道了,你可以忍受孤独