当前位置:网站首页>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;
}边栏推荐
- ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
- Redis设计规范
- matlab特征点提取--记录自用
- [esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
- The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
- Time series analysis 41 - time series prediction tbats model
- 我用小程序容器让移动研发效率提升了5倍!
- arthas使用教程
- 头文件库文件
- _HUGE and __IMP__HUGE in “math.h“
猜你喜欢

Thinking and summary of technical personnel | R & D Efficiency

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03

pkg打包node工程

Basic examples that must be mastered by beginners of C #

图解 3 种主流企业架构模式(建议收藏!)

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

21. 合并两个有序链表

关于软考高级要不要报班学习

How to get more marks in the game under the new economic model of Plato farm

博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)
随机推荐
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations
软件设计师考前20问,注意啦!!
JWT login authentication + token automatic renewal scheme, well written!
Take you to wechat applet development in 3 minutes
In retaliation for the dismissal of the company, I changed all code comments of the project!
OSPF的不规则区域,LSA和序列号
安装gmp
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号
redis的基础知识
j s的数组方法,循环
Mobile number, fixed line regular expression
Redis面试题必知必会
二维前缀和
广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!
PHP 获取接口的方式
腾讯技术专家:解密亿级用户产品 微信、QQ、王者荣耀...全面上云实践!
B2B2C系统亮点是什么?如何助力珠宝首饰企业打造全渠道多商户商城管理体系
matlab特征点提取--记录自用
Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
10分钟快速入门EVS【玩转华为云】