当前位置: 代码迷 >> 综合 >> 215: 数组中的第K个最大元素(快速排序、堆排序、选择排序、冒泡排序、计数排序)
  详细解决方案

215: 数组中的第K个最大元素(快速排序、堆排序、选择排序、冒泡排序、计数排序)

热度:53   发布时间:2023-09-29 22:34:17.0

1:直观思路,排序后找

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

2:利用大根堆nlog(k),亲测没排序快?

    public int findKthLargest(int[] nums, int k) {PriorityQueue<Integer> heap =new PriorityQueue<>((n1, n2) -> n1 - n2);for (int n: nums) {heap.add(n);if (heap.size() > k){heap.poll();}}return heap.poll();}

3:快速排序

4:冒泡排序

5:选择排序

6:计数排序

7:堆排序

  相关解决方案