美文网首页
求数组中第K大的值

求数组中第K大的值

作者: 昵称叫着玩 | 来源:发表于2019-08-29 11:37 被阅读0次
var findKthLargest = function (nums, k) {
    for (let i = 0; i <= k; i++) {
        let max = i
        for (let j = i; j < nums.length; j++) {
            if (nums[j] > nums[max]) max = j
        }
        swap(nums, i, max)
    }
    return nums[k - 1]
};

function swap(arr, a, b) {
    let tmp = arr[a]
    arr[a] = arr[b]
    arr[b] = tmp
}
仅供参考!

相关文章

网友评论

      本文标题:求数组中第K大的值

      本文链接:https://www.haomeiwen.com/subject/avzbectx.html