Very simple java solution beats 80%


  • 1
    B
    public int findKthLargest(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0 || k > nums.length) {
            return -1;
        }
        
        Arrays.sort(nums);
        
        int N = nums.length;
        
        return nums[N - k];
    }

  • 1
    K

    Much better I used Collections.sort with a boxed integer array - that beats 50% - your solution is surely better and simple. in fact this one is fine too. Value of k is always guaranteed to be in range so you can ignore those checks.

    public class Solution {
     public int findKthLargest(int[] nums, int k) {
         if (nums == null || nums.length == 0) return -1;
        Arrays.sort(nums);
        return nums[nums.length-k];
    }}

Log in to reply
 

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