Java Kth largest element in an array


  • 0
    M
    class Solution {
        public int findKthLargest(int[] nums, int k) {
            int n=0;
            while(n <k){
                for(int i = n+1; i< nums.length; i++){
                    if(nums[i]> nums[n]){
                        int temp = nums[n];
                        nums[n] = nums[i];
                        nums[i] = temp;
                    }
                }
                n ++;
            }
            return nums[k-1];
        }
    }
    

Log in to reply
 

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