【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 ...
随机推荐
- 2、FreeRTOS任务相关API函数
1.任务相关的API函数 函数存在于task.c中,主要的函数有: xTaskCreate():使用动态的方法创建一个任务: xTaskCreatStatic():使用静态的方法创建一个任务(用的非常 ...
- 09-JavaScript之伪数组arguments
JavaScript之伪数组arguments arguments代表的是实参.有个讲究的地方是:arguments只在函数中使用. 1.返回函数实参的个数 使用argument.length方法返回 ...
- sqlalchemy常用
一.SQLAlchemy 创建表 from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Colu ...
- 家庭记账本小程序之删(java web基础版四)
实现删除消费账单 1.main_left.jsp中该部分,调用Servlet中delete方法 2.Servlet中delete方法,调用Dao层list方法,跳转到del.jsp页面 3.Dao层l ...
- css清除默认样式,stylus学习
reset.css html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, ...
- Data Governance
https://erwin.com/blog/data-preparation-mapping/
- Spring MVC 使用介绍(十)—— 编码
一.概述 客户端与服务器端在交互过程中,需要将字符以某种编码方式转化为字节流进行传输,因此涉及字符的编码和解码.某一方以编码方案A编码,另一方须以同样的编码方案解码,否则会出现乱码. 客户端与服务器端 ...
- Flask websocket
websocket 概念 是一套协议,协议规定了: - 连接时需要握手 - 发送数据进行加密 - 连接之后不断开 意义 实现长轮询等操作 框架支持 - flask,gevent-websocket - ...
- Magento 2 安装数据表
Magento 2 安装数据表 第1步:安装脚本 首先,我们将为CRUD模型创建数据库表.为此,我们需要插入安装文件 app/code/Mageplaza/HelloWorld/Setup/Insta ...
- 利用 UDF 轻松迁移
位运算.布尔运算和逐位运算的乐趣 如果您正从支持位运算.布尔运算以及一些函数的数据库迁移到 IBM® DB2® Universal Database™(UDB),那么您也许会对如何在 DB2 中处理这 ...