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. python类的全面介绍

    转载:全面介绍python面向对象的编程——类的基础 转载:类的实例方法.静态方法.类方法的区别

  2. Python工程化小结

    对如何写一个工业级的Python项目作一个top-down小结. 一.项目结构 顶层结构: 文件夹: model可以是项目中的自定义类: utils是一些工程工具,比如log,tracker log存 ...

  3. MariaDB MaxScale

    1. down https://mariadb.com/downloads/#mariadb_platform-mariadb_maxscale (1) install sudo yum locali ...

  4. Structrued Streaming业务数据实时分析

    先启动spark-shell,记得启动nc服务 输入以下代码 scala> import org.apache.spark.sql.functions._ import org.apache.s ...

  5. Windows Server 2012R2 网络地址转换NAT

    一.NAT概述 网络地址转换NAT(Network Address Translation)可以动态改变通过路由器的IP报文的内容(修改报文的源IP地址和/或目的IP地址).离开路由器的报文的源地址或 ...

  6. python 打印调用栈

    import traceback def BBQ(): traceback.print_stack() 引入 traceback 包,在某个函数中执行 traceback.print_stack().

  7. jieba gensim 用法

    简单的问答已经实现了,那么问题也跟着出现了,我不能确定问题一定是"你叫什么名字",也有可能是"你是谁","你叫啥"之类的,这就引出了人工智能 ...

  8. swagger常用注解

    @Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数描述 @ApiModel:用对象来接收参数 @Api ...

  9. 文件替换(交互式)Replace

    在增量发版博文里,自动识别目标文件路径是采用了标记目录特征.当部署环境多变时,会多有不便.这个替换程序,在使用时需要手动输入目标目录.当需要进一步自动化时,可以与 fint 配合使用. 程序发布时,需 ...

  10. python:获取访问访问时的响应时间

    import time import os from datetime import datetime from selenium import webdriver from selenium.web ...