public int[][] merge(int[][] arr) { //根据第一个元素排序,快速排序 if(arr.length == 0) return new int[0][0]; if(arr.length == 1) return arr; boolean[] isvristed = new boolean[arr.length]; for(int i= 0 ; i < isvristed.length ; i++) { isvristed[i] = false; } List<int[]> list = new ArrayList<>(); boolean flag = false; quickSort(arr,0,arr.length-1); int i = 0; while(i< arr.length && i+1<arr.length) { int[] num1 = arr[i]; int[] num2 = arr[i+1]; isvristed[i] = true; isvristed[i+1] = true; while(true) { if(num1[1] >= num2[0]) { num1 = new int[] {num1[0] , Math.max(num1[1], num2[1])}; i++; arr[i] = num1; flag = true; } else { flag = false; break; } if(i< arr.length && i+1<arr.length) { num1 = arr[i]; num2 = arr[i+1]; isvristed[i] = true; isvristed[i+1] = true; } else { break; } } list.add(num1); i++; } if(flag == false) { list.add(arr[arr.length-1]); } int[][] ans = new int[list.size()][2]; int index = 0; for(int[] a : list) { ans[index][0] = a[0]; ans[index][1] = a[1]; index++; } return ans; } private void quickSort(int[][] arr, int low, int high) { if (low < high) { int index = quickSorts(arr,low,high); quickSort(arr, low, index - 1); quickSort(arr, index + 1, high); } } public int quickSorts(int[][] arr,int l , int r) { /** *1,设置锚点 *2,定义左右边界 **/ int[] curArr = arr[l]; int curValue = curArr[0]; while(l < r) { while(l < r && arr[r][0] >= curValue) { r--; } if(l < r) { arr[l] = arr[r]; l++; } while(l < r && arr[l][0] <=curValue) { l++; } if(l < r) { arr[r] = arr[l]; r--; } } arr[r] = curArr; return r; }
原文:https://www.cnblogs.com/swqblog/p/12865176.html