版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjkC050818/article/details/79432853
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.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
package go.jacob.day0303.array;
public class P215_KthLargestElementInAnArray {
public int findKthLargest(int[] nums, int k) {
int start = 0, end = nums.length - 1, target = nums.length - k;
int index;
while (start < end) {
index = partition(nums, start, end);
if (target > index)
start = index + 1;
else if (target < index)
end = index - 1;
else
return nums[index];
}
return nums[start];
}
private int partition(int[] a, int left, int right) {
int i = left, j = right + 1;
int tmp = a[left];
while (i < j) {
while (a[++i] < tmp) {
if (i == right)
break;
}
while (a[--j] > tmp) {
if (j == left)
break;
}
if (i >= j)
break;
exch(a, i, j);
}
exch(a, j, left);
return j;
}
private static void exch(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}