当前位置:网站首页>leetcode076——数组中的第 k 大的数字
leetcode076——数组中的第 k 大的数字
2022-07-28 09:54:00 【Schuyler_yuan】
题目:
给定整数数组
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 <= 104
-104 <= nums[i] <= 104
思路:
最直观的解法是把数组排序后,输出length-k位置的数即可。但这样做肯定存在不必要的操作。
快排算法可以参考:快慢指针实现快排
如何优化?
int findKthLargest(vector<int>& nums, int k) {
quicksort(nums, 0, nums.size() - 1);
return nums[nums.size() - k];
}优化思路:
利用快排的思路,每一轮partition都会找到一个轴值对应排序后的位置,当这个轴值最后的位置是k的时候,就直接输出即可,不用做完所有比较,最好的情况就是每次轴值都在中值位,二分快排。
代码如下:
int findKthLargest(vector<int>& nums, int k) {
int start = 0, end = nums.size() - 1;
int index = partition(nums, start, end);
while(index != nums.size() - k) {
if (index > nums.size() - k) {
end = index - 1;
index = partition(nums, start, end);
} else if (index < nums.size() - k) {
start = index + 1;
index = partition(nums, start, end);
}
}
return nums[index];
}
int swap(vector<int>& nums, int left, int right) {
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
return 0;
}
int partition(vector<int>& nums, int start, int end) {
int index = (start + end) / 2;
swap(nums, index, end);
int small = start - 1;
for (index = start; index < end; index++) {
if (nums[index] < nums[end]) {
++small;
if (small != index) {
swap(nums, small, index);
}
}
}
++small;
swap(nums, small, end);
return small;
}边栏推荐
- 选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步
- LIBCMTD.lib
- Basic knowledge of redis
- 这种动态规划你见过吗——状态机动态规划之股票问题(中)
- 腾讯技术专家:解密亿级用户产品 微信、QQ、王者荣耀...全面上云实践!
- Bit.store, which has attracted much attention, is at a glance of the latest developments
- 2022 uni app parsing token standard - use jsrsasign - climb the pit
- 【学习笔记】border与period
- 【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
- LinkedList source massage, ah comfortable
猜你喜欢

Illustrate three mainstream enterprise architecture models (recommended collection!)

二分、三分、01分数规划 【第I弹】

OSPF的LSA及优化

Being on duty less than 8 hours a day and being dismissed? Tencent's former employees recovered 13million overtime pay, etc., and the court won a compensation of 90000 in the final judgment

Software testing and quality learning notes 2 - black box testing

广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!

JS promotion: the underlying principle of flat tiling

3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities

房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升

Digital construction of pharmaceutical industry is on the verge
随机推荐
Status Notice ¶
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations
How to get more marks in the game under the new economic model of Plato farm
(10) Defer keyword
头文件库文件
[esp32][esp idf] ap+sta realizes wireless bridging and transferring WiFi signals
Which strings will be resolved to null by fastjason?
工业品MRO采购网站有哪些优势?一文带你读懂
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号
Deepin 下安装 LAMP
判断字符串是不是回文
Flink - checkpoint Failure reason: Not all required tasks are currently running
2022 uni app parsing token standard - use jsrsasign - climb the pit
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
How PHP gets the interface
二分、三分、01分数规划 【第I弹】
今天和大家聊一聊mysql数据库的数据类型
Sizebasedtriggingpolicy introduction
第四步-用户开发环境设置
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器