Skip to content

215两数和

code

javascript
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findKthLargest = function(nums, k) {
    let maxheap = [];
    for( let i = 0; i < nums.length; i++){
        if(maxheap.length < k){
            maxheap.push(nums[i]);
        }else{
            maxheap.sort((a,b) => a - b);
            console.log(maxheap);
            if(nums[i] > maxheap[0]){
                maxheap.shift();
                maxheap.push(nums[i]);
            } else{
                continue;
            }
        }
    }
    maxheap.sort((a,b) => a - b);
    return maxheap[0];  
};

总结