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.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

解答

用堆排序就好了……不过这个堆是从0开始的,还有点不适应orz

int findKthLargest(int* nums, int numsSize, int k) {
    int i, j, tmp, largest;

    ) / ; i >= ; i--){
         +  < numsSize){
             +  < numsSize&&nums[i *  + ] < nums[i *  + ]){
                largest = i *  + ;
            }
            else{
                largest = i *  + ;
            }
            if(nums[i] < nums[largest]){
                tmp = nums[i];
                nums[i] = nums[largest];
                nums[largest] = tmp;
                i = largest;
            }
            else{
                break;
            }
        }
    }
    ; j < k - ; j++){
        nums[] = nums[numsSize - ];
        numsSize--;
        i = ;
         +  < numsSize){
             +  < numsSize&&nums[i *  + ] < nums[i *  + ]){
                largest = i *  + ;
            }
            else{
                largest = i *  + ;
            }
            if(nums[i] < nums[largest]){
                tmp = nums[i];
                nums[i] = nums[largest];
                nums[largest] = tmp;
                i = largest;
            }
            else{
                break;
            }
        }
    }
    ];
}

LeetCode OJ 215. Kth Largest Element in an Array的更多相关文章

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

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

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

  4. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 使用unbound提供DNS域名解析服务

    使用unbound提供DNS域名解析服务 # 作者:Eric # 微信:loveoracle11g # 先配yum仓库 [root@server1 ~]# cd /etc/yum.repos.d/ [ ...

  2. HDFS 入门介绍

    HDFS简介 HDFS(Hadoop Distributed File System,Hadoop分布式文件系统),它是一个高度容错性的系统,适合部署在廉价的机器上.HDFS能提供高吞吐量的数据访问, ...

  3. ORACLE 通过连接查询更新 update select

    注意:  关键的地方是where 语句的加入. 在11G中, 如果不加11G , 或造成除匹配的行数更新为相应的值之后, 其余的会变成负数. 所以,执行前需要测试, 普通办法就是:  先查看需要更新的 ...

  4. CRM 报表中 Filtered 无数据

    数据源设置为以下才可以使用  Filtered+实体名称和  fn_FindBusinessGuid   和 fn_FindUserGuid 

  5. pthread mutex

    pthead_mutex_t mutex; 1:create: pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t ...

  6. javaweb项目中遇到的一些乱码问题

    在做javaweb项目时,我们经常会遇到一些乱码问题: 首先,确定一点思想:要想不乱码,你要保证编码一致就行了,即统一编码~ 其一,jsp等页面中的中文显示乱码(这里不只是说jsp文件,其它文件也有这 ...

  7. better-scroll高度问题

    better-scroll无法滚动主要2方面原因,1)层级关系.2)高度问题

  8. 快速部署MySQL数据库

    一.下载对应的软件版本 下载地址:http://mirrors.sohu.com/mysql/MySQL-5.6/ [root@localhost ~]# wget -q http://mirrors ...

  9. css动画animation-keyframes

    随着css3的流行,现在很多可以使用css3实现的动画效果,基本上就选择css3实现,尤其是在移动端的(移动端对css3的支持度相对比较高,PC端有很多IE8及以下的浏览器拖着后腿呢). 最近做了一个 ...

  10. 安全测试6_Web安全工具第一节(浏览器入门及扩展)

    今天来学习下浏览器的功能,浏览器是我们经常用到但是功能却很强大的一个东东,我们经常用到的无非是三种(谷歌.火狐.IE) 1.浏览器功能介绍: 下面以谷歌浏览器(Chrome版本为56)为例,介绍下,懂 ...