LeetCode-数组中的第K个最大元素

题目

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
示例 2:

输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4
说明:

你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。

https://leetcode-cn.com/problems/kth-largest-element-in-an-array

方案

采用QuickSort的思想来解决:

import java.util.Random;
class Solution {
    private int[] nums;
    private int partition(int l, int r, int pivot_index) {
        int pivot = this.nums[pivot_index];
        if (pivot_index != (r - 1)) {
            this.nums[pivot_index] = this.nums[r -1];
            this.nums[r - 1] = pivot;
        }
        int j = l;
        for (int i = l; i < r; i++) {
            if (this.nums[i] > pivot) {
                if (i != j) {
                    int temp = this.nums[i];
                    this.nums[i] = this.nums[j];
                    this.nums[j] = temp;
                }
                j++;
            }
        }
        this.nums[r - 1] = this.nums[j];
        this.nums[j] = pivot;
        return j;
    } 
    private int quickSelect(int low, int high, int k) {
        if (low == high) {
            return this.nums[low];
        }
        Random random_num = new Random();
        int pivot_index = low + random_num.nextInt(high - low); 
        int p = partition(low, high, pivot_index);
        if ((p + 1) == k) {
            return nums[p];
        } else if ((p + 1) > k) {
            return quickSelect(low, p, k);
        } else {
            return quickSelect(p + 1, high, k);
        }        
    }    
    public int findKthLargest(int[] nums, int k) {
        this.nums = nums;
        return quickSelect(0, nums.length, k);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值