LeetCode:数组中的第K个最大元素(Java)

221 阅读1分钟

一、数组中的第K个最大元素

给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。

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

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

二、基于冒泡排序的解法

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int tmp;
        for(int i=0; i<nums.length; i++){
            for(int j=1; j<nums.length-i; j++){
                if(nums[j-1]<nums[j]){
                    tmp = nums[j];
                    nums[j] = nums[j-1];
                    nums[j-1] = tmp;
                }
            }
        }
        // System.out.println(Arrays.toString(nums));
        return nums[k-1];
    }
}

image.png

时间复杂度:O(n2)O(n^2)

空间复杂度:O(1)O(1)

三、基于快速排序的解法

class Solution {
    public int findKthLargest(int[] nums, int k) {
        // int tmp;
        // for(int i=0; i<nums.length; i++){
        //     for(int j=1; j<nums.length-i; j++){
        //         if(nums[j-1]<nums[j]){
        //             tmp = nums[j];
        //             nums[j] = nums[j-1];
        //             nums[j-1] = tmp;
        //         }
        //     }
        // }
        // System.out.println(Arrays.toString(nums));
        quickSort(nums);
        return nums[k-1];
    }

    private static void quickSort(int[] nums){
        quickSort(nums, 0, nums.length-1);
    }

    private static void quickSort(int[] nums, int left, int right){
        if(left<right){
            int pos = partition(nums, left, right);
            quickSort(nums, left, pos-1);
            quickSort(nums, pos+1, right);
        }
    }

    private static int partition(int[] nums, int left, int right){
        swap(nums, left, (left+right)/2);
        int pivot = nums[left];
        int swapPos = left;
        for(int i=left+1; i<=right; i++){
            if(nums[i]>pivot){
                swapPos++;
                swap(nums, swapPos, i);
            }
        }
        swap(nums, swapPos, left);
        return swapPos;
    }

    private static void swap(int[] nums, int a, int b){
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}

image.png

  效果不错

三、基于堆排序的解法

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int heapSize = nums.length;
        buildMaxHeap(nums, heapSize);
        for (int i = nums.length - 1; i >= nums.length - k + 1; --i) {
            swap(nums, 0, i);
            --heapSize;
            maxHeapify(nums, 0, heapSize);
        }
        return nums[0];
    }

    public void buildMaxHeap(int[] a, int heapSize) {
        for (int i = heapSize / 2; i >= 0; --i) {
            maxHeapify(a, i, heapSize);
        } 
    }

    public void maxHeapify(int[] a, int i, int heapSize) {
        int l = i * 2 + 1, r = i * 2 + 2, largest = i;
        if (l < heapSize && a[l] > a[largest]) {
            largest = l;
        } 
        if (r < heapSize && a[r] > a[largest]) {
            largest = r;
        }
        if (largest != i) {
            swap(a, i, largest);
            maxHeapify(a, largest, heapSize);
        }
    }

    public void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

image.png

  • 优先权队列
class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
        for (int num : nums) {
            minHeap.add(num);
            if(minHeap.size()>k){
                minHeap.poll();
            }
        }
        return minHeap.peek();
    }
}

image.png

四、采用Arrays.sort()解决

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int len = nums.length;
        Arrays.sort(nums);
        return nums[len - k];
    }
}

image.png