LintCode5
来源:互联网 发布:软件开发流程文档 编辑:程序博客网 时间:2024/06/06 17:32
在数组中找到第k大的元素
注意事项
你可以交换数组中的元素的位置
给出数组 [9,3,2,4,8]
,第三大的元素是 4
给出数组 [1,2,3,4,5]
,第一大的元素是 5
,第二大的元素是 4
,第三大的元素是 3
,以此类推
代码:
class Solution {
public:
/*
* param k : description of k
* param nums : description of array and index 0 ~ n-1
* return: description of return
*/
void quicksort(vector<int> &a, int low, int high)
{
if (low >= high)
return;
int i = low;
int j = high;
int key = a[i];
while (i < j)
{
while (i < j && a[j] >= key)//找比key小的
j--;
a[i] = a[j];
while (i < j && a[i] <= key)//找比key大的
i++;
a[j] = a[i];
}
//循环结束,i==j
a[i] = key;/*pivot入位*/
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
int kthLargestElement(int k, vector<int> nums) {
quicksort(nums,0,nums.size()-1);
return nums[nums.size()-k];
// write your code here
}
};
- LintCode5
- lintcode5-5. 第k大元素
- CopyOnWriteArrayList
- linux 动态库的显示调用
- php禁止复制网页内容功能
- 比赛时需要注意的几个地方
- [LeetCode] 67. Add Binary
- LintCode5
- iOS 如何获取设备的各种信息
- supervisor ERROR (spawn error)
- android 6.0 logcat机制(三)logd处理请求log
- win10运行程序提示“为了对电脑进行保护,已经阻止此应用” 解决方法
- Django 学习笔记之 Class-Based-View
- C#之app.config、exe.config和vshost.exe.config作用区别
- RTandroid ADB MODE: Introduction to accessing the Raspberry Pi's GPIO in C++ (sysfs)
- Dss 现有的quicktime的文件结构和发包的处理机制