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. [原] Android快速开发框架-AndroidFine,GitHub开源

    Android快速开发框架 UI组件,不止是简单整合,更易用 沉浸式状态栏,界面更漂亮 左滑返回,非常流畅 简单.可复用.易扩展的底部导航 PagerSlidingTabStrip,导航标签文字颜色和 ...

  2. U盘操作系统,Kali Linux操作系统安装

    为什么要用U盘装操作系统,那好处多了去了.1.随身携带,想用就用.2.平常娱乐还是用Windows比较方便,不用做双系统那么麻烦. 准备: U盘,从天猫上买了个三星闪存盘,32G,USB3.0: 从官 ...

  3. HDOJ 4497 GCD and LCM

    组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  4. CSS样式案例(2)-制作一个简单的登录界面

    首先来张完工的效果图. 一.html文件如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. torch 入门

    torch 入门1.安装环境我的环境mac book pro 集成显卡 Intel Iris不能用 cunn 模块,因为显卡不支持 CUDA2.安装步骤: 官方文档 (1).git clone htt ...

  6. 在Xcode6.4中使用OpenCV

    XCode版本6.4,OpenCV版本3.0.0 昨天我安装完OpenCV之后,兴奋地按照这篇文章Mac平台上OpenCV开发环境搭建的步骤,在XCode上建了一个Demo工程,结果编译一直不成功.一 ...

  7. int long 等基础类型在不同平台的大小

    转自http://www.cnblogs.com/yishuiliunian/archive/2011/03/18/1988244.html 上次腾讯面试,问我int和long分别几个字节,结果被鄙视 ...

  8. Knockout.Js案例一Introduction

    </strong></p> <p>Last name: <strong data-bind="text:lastName ">tod ...

  9. JQuery测手速小游戏-遁地龙卷风

    (-1)写在前面 我用的chrome49,jquery3.0,我得到过399分,信不信由你. (1)设计思路 两个p元素放在div里,每个p元素的高度和宽度都和div一样,当鼠标放在div上时,第一个 ...

  10. [转载]localhost与127.0.0.1的区别

    原文链接:http://blog.csdn.net/xifeijian/article/details/12879395 很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什 ...