215. Kth Largest Element in an Array
Problem
Given an integer array
nums
and an integerk
, return thek<sup>th</sup>
largest element in the array.Note that it is the
k<sup>th</sup>
largest element in the sorted order, not thek<sup>th</sup>
distinct element.
Solution
直接排序数组,返回倒数第k个值。
Code
1 | class Solution { |
Solution 2
采用优先级队列,将所有元素加入队列,采用倒序比较器。
挤出前k-1个值,然后返回第k个值。
Code
1 | class Solution { |