描述

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,

Given [3,2,1,5,6,4] and k = 2, return 5.

Note:

You may assume k is always valid, 1 ≤ k ≤ array's length.

分析

代码如下:

class Solution {
public:
int partition(vector<int>&nums,int p,int r)
{
int x = nums[r];
int i = p - 1;
for(int j = p; j != r; ++j)
if(nums[j] < x)
swap(nums[++i],nums[j]);
swap(nums[i + 1],nums[r]);
return i + 1;
}
int findKthLargest(vector<int>& nums, int k) {
/*
sort(nums.begin(),nums.end());
return nums[nums.size() - k];
*/
int p = 0,r = nums.size() - 1;
while(true){
int q = partition(nums,p,r);
if(q < nums.size() - k)
p = q + 1;
else if(q > nums.size() - k)
r = q - 1;
else
return nums[q];
}
}
};

leetcode解题报告(31):Kth Largest Element in an Array的更多相关文章

  1. 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 ...

  2. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  3. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  4. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  5. Leetcode 之 Kth Largest Element in an Array

    636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...

  6. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  7. 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 ...

  8. 【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 ...

  9. 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 ...

  10. 【刷题-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 ...

随机推荐

  1. javascript 同源策略和 JSONP 的工作原理

    同源策略 同源策略是一个约定,该约定阻止当前脚本获取或操作另一域的内容.同源是指:域名.协议.端口号都相同. 简单地说,A 服务器下的 a 端口执行 ajax 程序,不能获取 B 服务器或者 A 服务 ...

  2. 转:Java接口和抽象类

    转:http://www.cnblogs.com/dolphin0520/p/3811437.html 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法:它只有声明,而没有 ...

  3. Sbase数据库自动截断日志

    http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36273.1550/html/sprocs/X3 ...

  4. hadoop2.8 集群 1 (伪分布式搭建)

    简介: 关于完整分布式请参考: hadoop2.8 ha 集群搭建   [七台机器的集群] Hadoop:(hadoop2.8) Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户 ...

  5. [LeetCode] 64. 最小路径和 ☆☆☆(动态规划)

    描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入:[  [1,3,1], [1,5,1 ...

  6. 微信小程序---客服消息接口调用,拿来即用

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 如果对你有帮助的话麻烦点个[推荐]~最好还可以follow一下我的GitHub~感谢观看! 在 ...

  7. MySQL中使用函数时,与后面括号不能之间不能根空格

    修改前代码: select MAX (article_order) from mall_school_article where 1=1 and is_deleted = 0 and status = ...

  8. C 语言究竟是一门怎样的语言?

    C语言是计算机及其相关专业的必修课,很多编程爱好者也是从C语言开始的编程之旅. C语言之父镇楼! 相较于GO,python等语言而言C语言真可以算是老古董了(发明于上世纪70年代),但是也正因为其历史 ...

  9. Vue路由实现页面跳转的两种方式(router-link和JS)

    Vue.js 路由可以通过不同的 URL 访问不同的内容,实现多视图的单页 Web 应用 1.通过 <router-link> 实现 <router-link> 组件用于设置一 ...

  10. djangCrm

    ---恢复内容开始--- 一> 在数据库进行循环取多对多 def get_classlist(self): l=[] for cls in self.class_list.all(): l.ap ...