LeetCode 215 - Kth Largest Element in an Array

Find the kth largest element in an unsorted array.

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.

public int findKthLargest(int[] nums, int k) {
    return findKthLargest(nums, 0, nums.length-1, k-1);
}

private int findKthLargest(int[] nums, int l, int h, int k) {
    if(k<l || k>h) return -1;
    int p = partition(nums, l, h);
    if(p == k) {
        return nums[p];
    } else if(p > k) {
        return findKthLargest(nums, l, p-1, k);
    } else {
        return findKthLargest(nums, p+1, h, k);
    }
}

private int partition(int[] nums, int l, int h) {
    int pivot = nums[h];
    int p = l;
    for(int i=l; i<h; i++) {
        if(nums[i]>pivot) {
            swap(nums, i, p++);
        }
    }
    swap(nums, p, h);
    return p;
}

private void swap(int[] nums, int i, int j) {
    int tmp = nums[i];
    nums[i] = nums[j];
    nums[j] = tmp;
}

 

你可能感兴趣的:(LeetCode)