LeetCode215-kth-largest-element-in-an-array
第k 大的元素
这题用递归出错了,划不来,一点都不好debug, 最重要的是抽线能力
class Solution { public int findKthLargest(int[] nums, int k) { mergeSort(nums, 0, nums.length-1); int count = nums.length; return nums[count-k]; } static public void mergeSort(int[] num, int left, int right){ if(left<right) { int mid = (left + right) / 2; mergeSort(num,left,mid); mergeSort(num, mid + 1, right); mergeSort(num, left, mid, right); } } static public void mergeSort(int[] num, int left, int mid, int right){ int[] sort = new int[num.length]; int counter = left; int i = left; int j = mid+1; while(i <= mid && j <= right){ if(num[i]>=num[j]){ sort[counter] = num[j]; j++; }else{ sort[counter] = num[i]; i++; } counter++; } while(i <= mid){ sort[counter++] = num[i++]; } while(j <= right){ sort[counter++] = num[j++]; } for(int k=left;k<=right;k++){ num[k] = sort[k]; } } }