【leetcode】Kth Largest Element in an Array (middle)☆
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)☆的更多相关文章
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 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 ...
- 【树】Kth Smallest Element in a BST(递归)
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 【LeetCode】812. Largest Triangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...
- LeetCode Kth Largest Element in an Array (快速排序)
题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...
- 【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 ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- EF上下文管理
- Mac 上真正替换LiveWriter 的工具 - ecto
Mac 上真正替换LiveWriter 的工具 - ecto 13年开始使用mac.而后想把 windows 替换到.一直在寻找LiveWriter 的工具,至今终于找到 我先感谢这位博主 http: ...
- Swift语法入门
正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...
- 第四天 rxcocoa
HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...
- linux C之getchar()非阻塞方式
参考链接: http://blog.csdn.net/zydlyq/article/details/50963360 #include "../include/CommUart.h" ...
- c#常用的Datable转换为json,以及json转换为DataTable操作方法
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- 2015年最有价值的30个响应式WORDPRESS主题
http://www.chinaz.com/design/2015/0521/408204.shtml 必须承认,Wordpress依然是目前最流行.最易用的内容管理系统,合理地使用Wordpress ...
- Linux文件读写机制及优化方式
导读 Linux是一个可控性强的,安全高效的操作系统.本文只讨论Linux下文件的读写机制,不涉及不同读取方式如read,fread,cin等的对比,这些读取方式本质上都是调用系统api read,只 ...
- Unity手游之路<十三>手游代码更新策略探讨
http://blog.csdn.net/janeky/article/details/25923151 这几个月公司项目非常忙,加上家里事情也多,所以blog更新一直搁置了.最近在项目开发上线过程中 ...
- unity销毁层级物体及 NGUI 深度理解总结
http://www.2cto.com/kf/201311/258811.html 1.想找到层级面板中某个物体,并销毁,利用下面的代码: GameObject obj = GameObjec ...