1. /**
    2. *最最最最典型的topK的问题,在这里先用优先队列实现一下子
    3. */
    4. public class Solution {
    5. public int findKthLargest(int[] nums, int k) {
    6. PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
    7. @Override
    8. public int compare(Integer o1, Integer o2) {
    9. return o1.compareTo(o2);
    10. }
    11. });
    12. for(int i : nums){
    13. if(priorityQueue.size() >= k){
    14. if(priorityQueue.peek() < i){
    15. priorityQueue.poll();
    16. priorityQueue.add(i);
    17. }
    18. }else {
    19. priorityQueue.add(i);
    20. }
    21. }
    22. return priorityQueue.poll();
    23. }
    24. }