【easy】215. Kth Largest Element in an Array 第K大的数
class Solution {
public:
int quicksort(vector<int>& nums, int start, int end, int k){
int i = start;
int j = end;
int x = nums[i];
while (i<j){ //快排核心…
while (nums[j]<x && i<j)
j--;
if (i<j)
nums[i++] = nums[j];
while (nums[i]>x && i<j)
i++;
if (i<j)
nums[j--]=nums[i];
}
nums[i] = x;
if (i==k-) return x;
else if (i>k-) //出错的地方……………………
return quicksort(nums,start,i-,k);
else
return quicksort(nums,i+,end,k);
}
public:
int findKthLargest(vector<int>& nums, int k) {
int len = nums.size();
//思路:快排,从大到小,放在第(K-1)处的就是第k大的
int res = quicksort(nums,,len-,k);
return res;
}
};
【easy】215. Kth Largest Element in an Array 第K大的数的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...
- 【LeetCode】215. Kth Largest Element in an Array (2 solutions)
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- 【刷题-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- leetcode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Java for LeetCode 215 Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- Python第二天: 变量详解及变量赋值
目录 什么是变量? 怎么写一个好的变量? 下划线命名法及驼峰命名法 结语 目录 此文章针对刚学Python的小白,若觉得对变量有很好的掌握,可以观看其他的文章 在这里, 我说一下我对变量的简单总结: ...
- 类别不平衡问题和Softmax回归
目录 类别不平衡(class-imbalance) Softmax回归模型 类别不平衡(class-imbalance) 当不同类别的训练样本数目差别很大,则会对学习过程造成困扰.如有998个反例,但 ...
- UIImagePickerController - 官方文档说明
使用UIImagePickerController对象的步骤: 1)验证设备是否能从目标源获取内容,通过调用 + (BOOL)isSourceTypeAvailable:(UIImagePickerC ...
- Linux 系统中五笔输入法有些字打不出来(已解决)
最近在使用CentOS7 桌面版本,在用五笔打字时,有些字打不出来,比如“覆盖”.但是在WIN下能打出来. 从网上查找原因,原来是需要改成GBK字符集.方法如下: 修改文件 vim /usr/shar ...
- Datatable get请求传参应用
以关注页面为例: html: <div class="row"> <div class="col-md-12 col-sm-12 col-xs-12&q ...
- fullpage.js参数参考
fullpage函数里面的参数: //Navigationmenu: false,//绑定菜单,设定的相关属性与anchors的值对应后,菜单可以控制滚动,默认为false.anchors:['fir ...
- DRF之频率限制、分页、解析器和渲染器
一.频率限制 1.频率限制是做什么的 开放平台的API接口调用需要限制其频率,以节约服务器资源和避免恶意的频繁调用. 2.频率组件原理 DRF中的频率控制基本原理是基于访问次数和时间的,当然我们可以通 ...
- Go语言中的Iota
一.复习常量 提到Iota这个关键字,就必须要复习一下Go语言的常量. 1.Go语言的常量一般使用const声明 2.Go语言的常量只能是布尔型.数字型(整数型.浮点型和复数)和字符串型 3.Go语言 ...
- Codeforces #381(div2)
A.题目:http://codeforces.com/contest/740/problem/A 题意:现有n本书,买一本书需要花a元,两本书b元,三本书c元,问买够书是4的倍数所需要的最小花费 思路 ...
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...