Java Easy PriorityQueue Solution


  • 0
    Z

    public class Solution {

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> q=new PriorityQueue<Integer>(nums.length,new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2) {
                if(o1<o2) return 1;
                else if(o1>o2) return -1;
                else return 0;
            }
        });
    
        for(int s:nums){
            q.add(s);
        }
    
        for(int i=0;i<k-1;i++){
            q.poll();
        }
    
        return q.peek();
    }
    

    }


  • 0
    H

    can't you make a better format?


  • 0
    Z

    sorry for that. I have already modified the code.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.