class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
"""堆排序思想""" def heapify(array, start, end): while True:
max_pos = start #初始化最大值所在位置为目标所在
if start*2 + 1 <= end and array[max_pos] < array[start*2+1]:
# 如果左叶子节点存在,且大于目标值,则将最大值所在位置指向该节点所在位置
max_pos = start*2 + 1
if start*2 + 2 <= end and array[max_pos] < array[start*2+2]:
# 如果右叶子节点存在,且大于目标值,则将最大值所在位置指向该节点所在位置
max_pos = start*2 + 2
if max_pos == start:
# 如果目标即为最大,完成该节点堆化,跳出循环
break
# 交换位置,将最大值
array[start], array[max_pos] = array[max_pos], array[start]
start = max_pos # 建堆,只需要对前半节点堆化
for i in range(len(nums)//2-1, -1, -1):
heapify(nums, i, len(nums)-1)
# 排序,只需要循环K次,排序TOP K个节点
i = len(nums) - 1
while i > len(nums) - 1 - k:
nums[0], nums[i] = nums[i], nums[0]
i -= 1
heapify(nums, 0, i)
return nums[len(nums)-k]

215。数组中第K个最大元素(堆实现)的更多相关文章

  1. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  2. 寻找数组中第K频繁的元素

    问题是:给你一个数组,求解出现次数第K多的元素.当然leetcode上的要求是算法复杂度不能大于O(N*logN). 首先这个问题我先是在leetcode上看到,当时想了两种做法,做到一半都觉得不是很 ...

  3. Java实现 LeetCode 215. 数组中的第K个最大元素

    215. 数组中的第K个最大元素 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6 ...

  4. 215. 数组中的第K个最大元素 + 快速排序 + 大根堆

    215. 数组中的第K个最大元素 LeetCode-215 另一道类似的第k大元素问题:https://www.cnblogs.com/GarrettWale/p/14386862.html 题目详情 ...

  5. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  6. C语言:将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边。-在m行m列的二维数组中存放如下规律的数据,

    //将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边. #include <stdio.h> #define M 3 #define N 5 ...

  7. 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)

    题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...

  8. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

随机推荐

  1. static,private,final,abstract,protected

    1,static:静态变量:位于方法区中,只有一份,这个类的所有实例共享,不可以被继承 静态方法:直接通过类就能调用,静态方法中只能使用静态变量,不可以被继承 2,private:类不能用privat ...

  2. 不同SEO对长尾关键词的不同做法

    http://www.wocaoseo.com/thread-122-1-1.html      长尾关键词指的是除目标关键词能带来搜索流量的关键词称之长尾关键词,它为一般由几个词语或短语组成,而且随 ...

  3. WebApi之DOM的基本介绍

    1.1.1 什么是DOM ​ 文档对象模型(Document Object Model,简称DOM),是 W3C 组织推荐的处理可扩展标记语言(html或者xhtml)的标准编程接口. ​ W3C 已 ...

  4. 力扣Leetcode 21. 合并两个有序链表

    合并两个有序链表 将两个升序链表合并为一个新的升序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1-> ...

  5. 使用BeetleX在Linux下部署.NET多站点服务

    ​      在windows下常用IIS来部署.NET的多站点服务,但在Linux下就没这么方便了:虽然可以使用一些代理服务器如nginx,jexus等来反代或部署应用,但nginx对.NET应用的 ...

  6. OpenCV(Open Source Computer Vision Library)计算机视觉库

    OpenCV(最基本的滤波到高级的物体检测皆有涵盖) 简介: OpenCV 是跨平台的,可以在  Windows.Linux.Mac OS.Android.iOS 等操作系统上运行. OpenCV 的 ...

  7. MPL心得

    1.右值引用变量是个左值,把一个右值引用参数继续传递给其他函数调用时,需要使用std::forward否则会按照左值匹配 2.const T和T const在匹配模板参数的时候是相同的,而const ...

  8. Stack (30)(模拟栈,输出中间数用set)

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  9. UVA 11292-Dragon of Loowater (贪心)

    Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...

  10. Codeforces 1321C Remove Adjacent

    题意 给你一个字符串,字符\(s_i\)可以被伤处当且仅当\(s_{i-1}=s_i-1\)或\(s_{i+1}=s_i-1\).问最多能删几个字符. 解题思路 其实,有个很简单的做法就是从\(z\) ...