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.

思路:

堆。讲解:二叉堆

class Solution {
public:
//新插入i结点 其父节点为(i - 1) / 2
void MinHeapFixup(int a[], int i)
{
int j = (i - ) / ; //父节点
int temp = a[i];
while(j >= && i != )
{
if(a[j] <= temp) break;
a[i] = a[j];
i = j;
j = (i - ) / ;
}
a[i] = temp;
} //在最小堆中插入新数据nNum
void MinHeapAddNumber(int a[], int n, int nNum)
{
a[n] = nNum;
MinHeapFixup(a, n);
} //堆删除后的调整 从i节点开始调整,n为节点总数 从0开始计算 i节点的子节点为 2*i+1, 2*i+2
void MinHeapFixdown(int a[], int i, int n)
{
int j = * i + ;
int temp = a[i];
while(j < n)
{
if(j + < n && a[j + ] < a[j]) //在左右孩子中找最小的
j++;
if(a[j] >= temp)
break;
a[i] = a[j]; //把较小的子结点往上移动,替换它的父结点
i = j;
j = * i + ;
}
a[i] = temp;
} //在最小堆中删除数
void MinHeapDeleteNumber(int a[], int n)
{
swap(a[], a[n - ]);
MinHeapFixdown(a, , n - );
} int findKthLargest(vector<int>& nums, int k) {
int * a = new int[k]; //大小为k的最小堆
for(int i = ; i < nums.size(); ++i)
{
if(i < k)
{
MinHeapAddNumber(a, i, nums[i]); //插入数据
}
else if(nums[i] > a[]) //比已有的k个最大的数字大
{
MinHeapDeleteNumber(a, k);
MinHeapAddNumber(a, k - , nums[i]);
}
}
return a[];
}
};

用STL的堆:

 int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> p;
const int s(nums.size()); for (int i = ; i < s; ++i) p.push(nums[i]);
while (--k) p.pop(); return p.top();
}

堆的相关讲解:

http://www.cnblogs.com/flyoung2008/articles/2136485.html

【leetcode】Kth Largest Element in an Array (middle)☆的更多相关文章

  1. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  2. 215. Kth Largest Element in an Array(QuickSort)

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  3. 【树】Kth Smallest Element in a BST(递归)

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  4. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  5. 【LeetCode】368. Largest Divisible Subset 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...

  6. 【LeetCode】812. Largest Triangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...

  7. LeetCode Kth Largest Element in an Array (快速排序)

    题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...

  8. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  9. 【leetcode】Remove Duplicates from Sorted List II (middle)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. sql中的常见的全局变量

    select APP_NAME ( ) as w --当前会话的应用程序 select @@IDENTITY --返回最后插入的标识值 select USER_NAME() --返回用户数据库用户名 ...

  2. AngularJS API之toJson 对象转为JSON

    toJson()能把对象序列化为json 方法讲解 这个方法最多支持2个参数: angular.toJson(obj, pretty); obj 是想要转换的对象, pretty 可以调节格式化的样式 ...

  3. EF-error 0152: No Entity Framework provider found...

    突然就报这个错了... ... 哈哈··· 原来是 "EntityFramework.SqlServer.dll" 没有引用··· 添加引用就好了... ... 还好不了?那就不知 ...

  4. VTK初学一,Pro文件的配置

    1. pro文件的配置 TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG += qt QT += core gui greate ...

  5. git/gitLab

    gitlab安装:http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml http://www.cnblogs.com/wi ...

  6. 【PHP面向对象(OOP)编程入门教程】19.抽象方法和抽象类(abstract)

    在OOP语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法做为外部代码访问其的接口.而抽象方法就是为了方便继承而引入的,我们先来看一下抽象类和抽象方法的定义再说明它的用途. 什么是抽象方 ...

  7. Windows 8 IIS中配置PHP运行环境的方法

    在Windows 8 的IIS(8.0)中搭建PHP运行环境: 一:安装IIS服务器 1.进入控制面板>>程序和功能>>打开或关闭Windows 功能,找到Internet信息 ...

  8. Maven的依赖范围

    Maven的依赖构件包含一个依赖范围属性,这个属性描述的是三套classpath的控制,即编译.测试.运行. 举个例子Junit依赖只是在测试范围(classpath)使用,而在运行的时候不使用,还有 ...

  9. 在应用中嵌入Python:转

    在应用中嵌入Python 前面的章节讨论如何扩展Python,如何生成适合的C库等.不过还有另一种情况:通过将Python嵌入C/C++应用以扩展程序的功能.Python嵌入实现了一些使用Python ...

  10. Github如何删除repository(仓库)

    首先就是你的Github主页了. 第二步点击进入一个repository(仓库) 第三步点击右上的setting 将此页面滑动到最下面找个这个 点击删除即可!