Simple solution by using the Priorityqueue


  • 0
    A

    Just using priorityqueue to solve this question

     static class sortnum implements Comparator<Integer> {
           public int compare(Integer one, Integer two) {
    			return two - one;
    		}
    	}
    public int findKthLargest(int[] nums, int k) {
            int res = 0;
            sortnum bigtosmall = new sortnum();
            PriorityQueue<Integer> sortqueue = new PriorityQueue<Integer>(nums.length,bigtosmall);
            for(int num : nums){
                sortqueue.offer(num);
            }
            int i = 0;
            while(i<k&&!sortqueue.isEmpty()){
                res = sortqueue.poll();
                i++;
            }
            return res;
    

    Have fun :)


Log in to reply
 

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