Description:
Find K-th largest element in an array.

Note:

用了heapSort的思想。需要注意的是调用vector的size()函数返回的是unsigned int类型,当为负时会越界,所以要转换为int类型。

class Solution {
public:/** param k : description of k* param nums : description of array and index 0 ~ n-1* return: description of return*/void maxify(vector<int>& num, int parent ,int end) {int leftchild = (parent + 1) * 2 - 1;int rightchild = (parent + 1) * 2;int maxChild;if (leftchild > end)return;if (rightchild > end || num[leftchild] > num[rightchild])maxChild = leftchild;elsemaxChild = rightchild;if (num[parent] < num[maxChild]) {swap(num[parent], num[maxChild]);maxify(num, maxChild,end);}}void buildHeap(vector<int>& num) {int numsize = num.size();int i = numsize / 2;for (; i >= 0; i--) {maxify(num, i, numsize - 1);}}public:int kthLargestElement(int k, vector<int> nums) {// write your code herebuildHeap(nums);for(int i = nums.size()-1;i>(int)nums.size()-k-1;i--){swap(nums[0],nums[i]);maxify(nums,0,i-1);}return nums[nums.size()-k];}
};

LintCode 5: Kth Largest Element相关推荐

  1. Lintcode - kth largest element

    Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3 ...

  2. Leetcode: 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 ...

  3. LeetCode 215. Kth Largest Element in an Array--数字第K大的元素--最大堆或优先队列--C++,Python解法

    题目地址:Kth Largest Element in an Array - LeetCode Find the kth largest element in an unsorted array. N ...

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

  5. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  6. [leedcode 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 ...

  7. 49.Kth Largest Element in an Array

    Level:   Medium 题目描述: Find the kth largest element in an unsorted array. Note that it is the kth lar ...

  8. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

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

  9. [swift] 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 ...

最新文章

  1. 解决WAS报错SRVE0207E: servlet 创建了未捕获到的初始化异常
  2. matlab多径误差包络,MIMO-OFDM系统添加多径信道仿真结果误差很大
  3. 关于微信公众平台表情代码的记录
  4. twitter推文不收录_如何使用Twitter书签保存推文供以后使用
  5. codeforces gym-101745 D-Stamp Stamp Stamp动态规划
  6. leetcode面试题 16.26. 计算器(栈)
  7. pcfg 自然语言处理_自然语言处理导航
  8. 计算机系统中CPU的寄存器介绍
  9. 数据化管理第一步,你真的会做报表吗?
  10. cesium获取点击内容信息_Cesium获取鼠标点击位置(PickPosition)
  11. go处理get、post请求返回的body数据
  12. java随机点名器的代码_巧用Excel制作随机点名器,简单易学,快来尝试吧
  13. 职称计算机培训一天,职称计算机的培训心得
  14. 点击电脑版微信一直打不开解决方案
  15. Juniper JNCIS-ER jn0-350 148Q
  16. 快速准确读取发票信息——发票扫描识别系统
  17. 【手机下载Linux】
  18. java类的心得_java面向对象学习心得3篇
  19. 单元格下拉全选快捷键_【excel下拉全选快捷键】Excel中全选是Ctrl A,那么反选呢?...
  20. 我有八千元,买苹果手机还是安卓呢?

热门文章

  1. 计算机专业四大名校排名,美国CS专业四大名校!总有一款适合你!
  2. 2020央视元宵晚会 | 《你的样子》朗诵词
  3. python3+正则(re)增量爬虫爬取笔趣阁小说( 斗罗大陆IV终极斗罗)
  4. Navicat for Mysql注册(Navicat 8.2 MySQL 8.2.17注册码)写入注册码后,点不动确认键
  5. 史玉柱10大广告法则
  6. 第1期-通过携程爬取机票价格
  7. 包子笔记 - 三知道原则
  8. 隐式转换、IF判断、逻辑【与、或、非】、三目运算的用法(基础)
  9. 机器学习(0):机器学习概述及基本概念
  10. Dijkstra算法和Floyd算法对比分析