215. 数组中的第K个最大元素 的快速排序 的 快速排序思路
java语言
class Solution {
public int findKthLargest(int[] nums, int k) {
quickPartition(nums,0,nums.length-1);
int len = nums.length;
return nums[len-k];
}
private void quickPartition(int[] arr,int left,int right){
if(left > right){
return;
}
int pivotIndex = partition(arr,left,right);
quickPartition(arr,left,pivotIndex-1);
quickPartition(arr,pivotIndex+1,right);
}
private int partition(int[] arr,int left ,int right){
int pivotValue = arr[left];
int holdIndex = left;
int leftIndex = left;
int rightIndex = right;
while(leftIndex < rightIndex){
while(leftIndex < rightIndex && arr[rightIndex] >= pivotValue){
rightIndex--;
}
arr[holdIndex] = arr[rightIndex];
holdIndex = rightIndex;
while(leftIndex < rightIndex && arr[leftIndex] <= pivotValue){
leftIndex++;
}
arr[holdIndex] = arr[leftIndex];
holdIndex = leftIndex;
}
arr[holdIndex] = pivotValue;
return holdIndex;
}
}
这段代码确实完成了快排且根据题目要求得到了结果,但是我们发现 target的大小是变量 如果target大于小于等于int pivotIndex呢?
这里对target进行分类讨论
class Solution {
public int findKthLargest(int[] nums, int k) {
int len = nums.length - k;
return quickPartition(nums,0,nums.length-1,len);
}
private int quickPartition(int[] arr,int left,int right,int target){
if(left > right){
return -1;
}
int pivotIndex = partition(arr,left,right);
// 这里是关键
if(target == pivotIndex){
return arr[pivotIndex];
}else if(target > pivotIndex){
return quickPartition(arr,pivotIndex+1,right,target);
}else{
return quickPartition(arr,left,pivotIndex-1,target);
}
}
private int partition(int[] arr,int left ,int right){
int pivotValue = arr[left];
int holdIndex = left;
int leftIndex = left;
int rightIndex = right;
while(leftIndex < rightIndex){
while(leftIndex < rightIndex && arr[rightIndex] >= pivotValue){
rightIndex--;
}
arr[holdIndex] = arr[rightIndex];
holdIndex = rightIndex;
while(leftIndex < rightIndex && arr[leftIndex] <= pivotValue){
leftIndex++;
}
arr[holdIndex] = arr[leftIndex];
holdIndex = leftIndex;
}
arr[holdIndex] = pivotValue;
return holdIndex;
}
}
上两个代码中还存在一个边界条件没有考虑到当存在大量重复值的时候会进行多次无意义的比较(官解思路)
## 个例子,大量重复:x=5,数组 [5,5,5,5,5]
1. i 从左往右:do i++,然后判断 nums [i] <5?不成立 → i 停下
2. j 从右往左:do j--,然后判断 nums [j] >5?不成立 → j 停下
3. i<j,交换两个 5(交换本身没改变数组),然后 i++、j--
👉 等于 pivot 的元素,**会被持续把 i、j 向中间挤**,相等的元素分散在中间,而不是全部堆在左半边或者右半边。
class Solution {
public int findKthLargest(int[] nums, int k) {
// 找第k大 → 转化:升序下标 target = n - k
int n = nums.length;
return quickSelect(nums, 0, n - 1, n - k);
}
private int quickSelect(int[] nums, int l, int r, int k) {
if (l >= r) return nums[l];
// 随机选pivot,再加一层防有序数组退化(双重保险)
int randIdx = l + (int)(Math.random() * (r - l + 1));
swap(nums, l, randIdx);
int x = nums[l];
int i = l - 1;
int j = r + 1;
while (i < j) {
do i++; while (nums[i] < x);
do j--; while (nums[j] > x);
if (i < j) {
swap(nums, i, j);
}
}
// 划分完:[l,j] ≤x,[j+1,r] ≥x
if (k <= j) {
return quickSelect(nums, l, j, k);
} else {
return quickSelect(nums, j + 1, r, k);
}
}
private void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
我的笔记并不精美,如果对你的阅读产生了误导,请不要恶语相向
评论 (0)


