【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/kth-largest-element-in-an-array/description/
题目描述
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
- You may assume k is always valid, 1 ≤ k ≤ array’s length.
题目大意
找出数组中第k大的值。
解题方法
方法一:移除最大值
找出第k大的数字。这个题可以直接排序,速度还挺快。我的做法是通过循环k-1
次,每次都在列表中去除列表的最大值。注意,列表的remove每次只会移除一个值。例如:
In [1]: a = [1,2,3,3,3,3]
In [2]: a.remove(3)
In [3]: a
Out[3]: [1, 2, 3, 3, 3]
In [4]: a.remove(3)
In [5]: a
Out[5]: [1, 2, 3, 3]
这题答案:
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
for i in range(k - 1):
nums.remove(max(nums))
return max(nums)
方法二:排序
排序之后直接找倒数第k个数字即可。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};
方法三:大顶堆
使用大顶堆,弹出k-1个数字,那么再弹一次就是第k大的值了。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> q;
for (int n : nums) {
q.push(n);
}
int res = 0;
while (k--) {
res = q.top(); q.pop();
}
return res;
}
};
方法四:partition
待完成。
日期
2018 年 2 月 5 日
2018 年 12 月 23 日 —— 周赛成绩新高
【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- [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 ...
- leetcode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Java for LeetCode 215 Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [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 ...
- LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...
- C#版 - Leetcode 215. Kth Largest Element in an Array-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- 8核cpu,,负载
今天有一个电话面试,面试官问我:CentOS怎么查看CPU负载?我说:看top的第一行有load average.面试官又问:为什么从这就判定是负载高呢?依据是什么呢?然后... 然后我就尴尬了,挂了 ...
- python—模拟生成双色球号和大乐透号
下边这个脚本,比较适合初级学习基本python语法用.但是,不精炼建议可参考https://www.cnblogs.com/Formulate0303/p/14031748.html的写法. 大乐透玩 ...
- mysql 不等于 符号写法
今天在写sql语句的时候,想确认下mysql的不等于运算符是用什么符号表示的 经过测试发现mysql中用<>与!=都是可以的,但sqlserver中不识别!=,所以建议用<> ...
- 在前端页面中使用Markdown并且优化a标签
近期在自己的项目中加入了对 Markdown 语法 的支持,主要用到的是markedjs这个项目.该项目托管在github上,地址为:https://github.com/markedjs/marke ...
- 学习java的第十天
一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Hive(十)【窗口函数】
目录 一.定义 窗口函数: 标准聚合函数 分析排名函数 二.语法 (1)窗口函数 over([partition by 字段] [order by 字段] [ 窗口语句]) (2)窗口语句 三.需求练 ...
- Apache2配置文件解读
每次碰到都不知道具体的作用,所以来分析一下 配置文件结构 apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后 ...
- Java程序逻辑控制
程序逻辑控制 1.Java程序结构与逻辑控制 在Java中程序有三种结构:顺序结构.分支结构.循环结构 1.1 Java分支结构 分支结构:进行逻辑判断,当满足某些条件时才会执行某些语句. 1.if语 ...
- Mysql 常见报错和疑问汇总
1.初始化数据库的时候报错 error while loading shared libraries: libstdc++.so.5: cannot open shared object file: ...