public class Solution {
public int findKthLargest(int[] nums, int k) {
int length = nums.length;
}
public int helper(int[] nums, int k, int start, int end){
int pivot = nums[end];
int left = start;
int right = end;
while(nums[left] <= pivot && left < right){
left ++;
}
while(nums[right] >= pivot && left < right){
right--;
}
if(left == right){
break;
}
swap(nums, left, end);
if (k == left + 1) {
return pivot;
} else if (k < left + 1) {
return getKth(k, nums, start, left - 1);
} else {
return getKth(k, nums, left + 1, end);
}
}
public void swap(int[] nums, int a, int b){
int tem = nums[a];
nums[a] = nums[b];
nums[b] = tem;
}
}
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
这道题和快速排序的思路有点像,都是找pivot 然后转换位置,会写quick sort 这个题应该没问题