当前位置:网站首页>力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
2022-08-04 03:02:00 【ChaoYue_miku】
给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
示例 1:
输入: [3,2,1,5,6,4], k = 2
输出: 5
示例 2:
输入: [3,2,3,1,2,4,5,5,6], k = 4
输出: 4
提示:
1 <= k <= nums.length <= 105
-104 <= nums[i] <= 104
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/kth-largest-element-in-an-array
方法一:快速排序
C++提交内容:
class Solution {
public:
int quickSelect(vector<int>& a, int l, int r, int index) {
int q = randomPartition(a, l, r);
if (q == index) {
return a[q];
} else {
return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index);
}
}
inline int randomPartition(vector<int>& a, int l, int r) {
int i = rand() % (r - l + 1) + l;
swap(a[i], a[r]);
return partition(a, l, r);
}
inline int partition(vector<int>& a, int l, int r) {
int x = a[r], i = l - 1;
for (int j = l; j < r; ++j) {
if (a[j] <= x) {
swap(a[++i], a[j]);
}
}
swap(a[i + 1], a[r]);
return i + 1;
}
int findKthLargest(vector<int>& nums, int k) {
srand(time(0));
return quickSelect(nums, 0, nums.size() - 1, nums.size() - k);
}
};
边栏推荐
- Big guys, it takes a long time to read mysql3 million single tables, what parameters can be discounted, or is there any way to hurry up
- 云开发校园微社区微信小程序源码/二手交易/兼职交友微信小程序开源源码
- Presto中broadcast join和partition join执行计划的处理过程
- DIY电工维修如何拆卸和安装开关面板插座
- ingress 待完善
- 架构实战营模块三作业
- STM8S105K4T6------串口发送和接收
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- 【原创】启动Win10自带的XPS/OXPS阅读器
- Engineering drawing review questions (with answers)
猜你喜欢

MySQL高级-读写分离-分库分表

Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu

自制蓝牙手机app控制stm8/stm32/C51板载LED

In the season of going overseas, the localization of Internet tips for going overseas

全网没有之一的JMeter 接口测试流程详解

Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon

Pine Script | How to display and typeset a plot switch?

Parquet encoding

Detailed analysis of scaffolding content

Example 041: Methods and variables of a class
随机推荐
说说数据治理中常见的20个问题
Day13 Postman的使用
Small Turtle Compilation Notes
KingbaseES数据库启动失败,报“内存段超过可用内存”
MySQL高级-读写分离-分库分表
C language -- ring buffer
[Original] Start the XPS/OXPS reader that comes with Windows 10
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
Example 039: Inserting elements into an ordered list
为什么用Selenium做自动化测试
web端动效 lottie-web 使用
【医保科普】维护医保基金安全,我们可以这样做
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
Instance, 038: the sum of the diagonal matrix
架构实战营模块三作业
APP电商如何快速分润分账?
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
cdh6.x 集成spark-sql
Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri