博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
K-th largest element in an array
阅读量:4438 次
发布时间:2019-06-07

本文共 681 字,大约阅读时间需要 2 分钟。

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  for adding this problem and creating all test cases.

 

Runtime: 12ms

1 class Solution { 2 public: 3     int findKthLargest(vector
& 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

你可能感兴趣的文章
C++primer 10.2.1节练习
查看>>
perl 执行mysql select 返回多条记录
查看>>
mojo 关闭utf8
查看>>
tomcat架构分析(valve机制)
查看>>
消息队列RabbitMQ基础知识详解
查看>>
接口、抽象类、方法复写、类Equals方法重写
查看>>
快学Scala习题解答—第十章 特质
查看>>
Ffmpeg 定位文件(seek file)
查看>>
数据结构与算法随学随记
查看>>
微软Azure已开始支持hadoop--大数据云计算
查看>>
统计_statistics_不同的人_大样本_分析_统计方法_useful ?
查看>>
wampserver 绑定域名 外部可以正常访问
查看>>
将博客搬至CSDN
查看>>
sqoop/1.4.6/下载
查看>>
https协议及与http协议的比较
查看>>
mongodb数据备份与恢复
查看>>
ubuntu安装(owncloud-docker安装)
查看>>
(十一)tina | openwrt关闭调试串口(DEBUG UART)
查看>>
Android中获取TextView行数
查看>>
AngularJS 学习笔记值post传值
查看>>