快排都快忘干净了(对自己无语😅
java:
class Solution {
public int findKthLargest(int[] nums, int k) {
return quicksort(nums, 0, nums.length - 1, nums.length - k);
}
private int quicksort(int[] nums, int p, int r, int k){
int q = partion(nums, p, r);
if(q == k){
return nums[q];
}else if(q > k){
return quicksort(nums, p, q-1, k);
}else{
return quicksort(nums, q + 1, r, k);
}
}
private int partion(int[] nums, int p, int r){
int j = p, k = p;
for(; k < nums.length - 1; k++){
if(nums[k] < nums[r]){
swap(nums, j, k);
j++;
}
}
swap(nums, j, r);
return j;
}
private void swap(int[] arr, int p, int index){
if(p == index){
return;
}
int tmp = arr[p];
arr[p] = arr[index];
arr[index] = tmp;
}
}