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.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Runtime: 12ms

 1 class Solution {
 2 public:
 3     int findKthLargest(vector<int>& nums, int k) {
 4         int n = nums.size();
 5         if(n == 0 || k > n) return 0;
 6
 7         sort(nums.begin(), nums.end());
 8         return nums[n - k];
 9     }
10 };

View Code

转载于:https://www.cnblogs.com/amazingzoe/p/4850618.html

K-th largest element in an array相关推荐

  1. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意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. 力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

    题目链接:https://leetcode.cn/problems/kth-largest-element-in-an-array 目录: 一.题目描述 1.中文 2.英文 二.解决方法 1.直接排序 ...

  4. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  5. 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. Leetcode: 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. [leedcode 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 ...

  8. 49.Kth Largest Element in an Array

    Level:   Medium 题目描述: Find the kth largest element in an unsorted array. Note that it is the kth lar ...

  9. [swift] 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 ...

  10. 《leetCode》: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 ...

最新文章

  1. 【java】浅谈注释
  2. 0基础该如何学Python?这些方法你必须了解
  3. 冲刺One之站立会议8 /2015-5-21
  4. 怎么用python处理excel文件-如何用python处理excel表格
  5. BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊
  6. C++面试宝典2011版
  7. Java程序员从笨鸟到菜鸟之(八十九)跟我学jquery(五)jquery中的ajax详解
  8. python批量删缩进_Python工具PyCharm常用快捷键
  9. html横向排列flex,flex布局示例
  10. 博士四年8篇CNS主刊论文,清华大学优秀科研团队叫做“沈飞党”
  11. 浅谈App对我们行业门户网站的作用
  12. PHP curl 使用代码
  13. IDM无法连接到目标服务器
  14. 博弈论中SG函数的解释与运用
  15. win7计算机打印机共享权限设置,win7共享打印机(没有权限访问)
  16. 查看计算机网卡型号命令,win10系统 查看电脑网卡型号的设置方法
  17. HDLBits 答案之Exams/ece241 2014 q7b
  18. 阿里云,AWS和DigitalOcean:云服务比较
  19. [车联网安全自学篇] Car Hacking之CAN总线初探
  20. Kotlin高仿微信-第14篇-单聊-视频通话

热门文章

  1. Eclipse或SVN—如何在Eclipse中安装SVN客户端插件
  2. Zend Studio 12.5.1 破解、注册码
  3. .Net获取URL中文参数值乱码问题
  4. C++11 std::function类模板
  5. mysql 存储过程使用参数_mysql 存储过程 使用参数
  6. sqlserver安装介质上文件的长路径名称失败_SQL Server 2012 软件安装教程
  7. 小型 web 服务器系统,小型WEB服务器 - 应用服务器是什么_应用服务器有哪些
  8. python 读取access_python读取数据access出错
  9. 西安后宰门小学_【云端论坛】走向未来的墙中老校 ——后宰门小学“名校+”教育集团云论坛精彩绽放...
  10. Java进阶:The driver is automatically registered via the SPI and manual loading of the driver class is