作者: 负雪明烛
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++)的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. C#版 - Leetcode 215. Kth Largest Element in an Array-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. kubernetes部署kube-controller-manager服务

    本文档介绍部署高可用 kube-controller-manager 集群的步骤. 该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态.当 leader ...

  2. mongoDB整个文件夹拷贝备份还原的坑

    现网有一个mongoDB数据库需要搬迁到新服务器,开发那边的要求是先搬迁现在的数据库过去,然后剩下的以后他们用程序同步. 数据库大楷20G左右,现网是主备仲裁的,停掉备点,拷贝了全部文件. 新服务器也 ...

  3. 码上来战!探索“智”感生活,HMS Core线上Codelabs挑战赛第4期开始!

    HMS Core线上Codelabs挑战赛第4期正式开始!我们向所有实践力超强.创新力满满的开发者发出邀请,用你的超级"码"力,解锁更多应用价值! 生活里,我们被手机"秒 ...

  4. 浅谈MySQL数据库面试必要掌握知识点

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 MySQL官方地址 https://www.mysql.com/ MySQL 8系列最新版本为8.0.27,5系列的最 ...

  5. A Child's History of England.37

    Many other noblemen repeating and supporting this when it was once uttered, Stephen and young Planta ...

  6. Elasticsearch【基础入门】

    目录 一.操作index 1.查看index 2.增加index 3.删除index 二.操作index 1.新增document 2.查询type 全部数据 3.查找指定 id 的 document ...

  7. [转] Java中对数据进行加密的几种方法

    加密算法有很多种:这里只大约列举几例: 1:消息摘要:(数字指纹):既对一个任意长度的一个数据块进行计算,产生一个唯一指纹.MD5/SHA1发送给其他人你的信息和摘要,其他人用相同的加密方法得到摘要, ...

  8. 【TCP/IP】之Java socket编程API基础

    Socket是Java网络编程的基础,深入学习socket对于了解tcp/ip网络通信协议很有帮助, 此文讲解Socket的基础编程.Socket用法:①.主要用在进程间,网络间通信. 文章目录如下: ...

  9. redis安装与简单实用

    1.在Linux上redis的安装时十分简单的: 第一步:wget http://download.redis.io/releases/redis-2.8.12.tar.gz 解压: tar zxvf ...

  10. 颜色RGB值对照表

    转载自 http://www.91dota.com/?p=49# 常用颜色的RGB值及中英文名称   颜  色    RGB值 英文名 中文名   #FFB6C1 LightPink 浅粉红   #F ...