544. Top k Largest Numbers【medium】
Given an integer array, find the top k largest numbers in it.
Given [3,10,1000,-99,4,100]
and k = 3
.
Return [1000, 100, 10]
.
解法一:
1 class Solution { 2 public: 3 /* 4 * @param nums: an integer array 5 * @param k: An integer 6 * @return: the top k largest numbers in array 7 */ 8 vector<int> topk(vector<int> nums, int k) { 9 std::priority_queue<int> heap; 10 for(int i = 0; i < nums.size(); i++) { 11 heap.push(nums[i]); 12 } 13 14 std::vector<int> result; 15 for (int i = 0; i < k; i++) { 16 result.push_back(heap.top()); 17 heap.pop(); 18 } 19 20 return result; 21 } 22 };
优先队列,类似于维护一个大小为k的最大堆/最小堆(STL中的priority_queue默认是最大堆),时间复杂度为O(n * logk)
解法二:
1 class Solution { 2 /* 3 * @param nums an integer array 4 * @param k an integer 5 * @return the top k largest numbers in array 6 */ 7 public int[] topk(int[] nums, int k) { 8 int[] temp = new int[k]; 9 if(nums == null || nums.length == 0) { 10 return temp; 11 } 12 quickSort(nums, 0, nums.length - 1, k); 13 14 if(nums.length < k) { 15 return nums; 16 } 17 18 for(int i = 0; i < k; i++) { 19 temp[i] = nums[i]; 20 } 21 22 return temp; 23 } 24 25 public void quickSort(int[] nums, int start, int end, int k) { 26 if (start >= end) { 27 return; 28 } 29 30 int left = start, right = end; 31 int mid = left + (right - left) / 2; 32 int pivot = nums[mid]; 33 34 while(left <= right) { 35 while(left <= right && nums[left] > pivot) { 36 left++; 37 } 38 39 while(left <= right && nums[right] < pivot) { 40 right--; 41 } 42 43 if(left <= right) { 44 swap(nums, left, right); 45 left++; 46 right--; 47 } 48 } 49 50 quickSort(nums, start, right, k); 51 quickSort(nums, left, end, k); 52 } 53 54 private void swap(int[] nums, int left, int right) { 55 int temp = nums[left]; 56 nums[left] = nums[right]; 57 nums[right] = temp; 58 } 59 };
快速排序,取出前k大的数,时间复杂度O(n*logn + k)
转载于:https://www.cnblogs.com/abc-begin/p/8151090.html
544. Top k Largest Numbers【medium】相关推荐
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- KNN最优k的选取【学习曲线】
怎样选择一个最佳的 k呢? 在这里我们要使用机器学习中的神器:参数学习曲线. 参数学习曲线是一条以不同的参数取值为横坐标,不同参数取值下的模型结果为纵坐标的曲线 # 1. 导入所需要的模块和库 imp ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】...
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...
- Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...
- HDU1492 The number of divisors(约数) about Humble Numbers【约数】
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- 最小的k个数 java_【Java】 剑指offer(40) 最小的k个数
本文参考自<剑指offer>一书,代码采用Java语言. 题目 输入n个整数,找出其中最小的k个数.例如输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 思 ...
- merge k sorted lists java_LeetCode 第23题 Merge k Sorted Lists【分而治之】【最小堆】(Java)...
这道题的题目是合并k个有序的链表,标定难度为Hard,详细需求: 合并k个有序的链表,返回一个新的有序链表. 例子: Input: 1->4->5, 1->3->4, 2-&g ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- UVA763 LA5339 Fibinary Numbers【大数】
The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the se ...
最新文章
- java 中ln是什么意思_JavaBean命名规范
- OpenCV 双边滤波
- 安装RVDS2.2破解版
- hadoop学习笔记2
- 聊城大学计算机学院韩玉艳,人工蜂群优化及其在资源管理中的应用.doc
- cellphonedb 及其可视化
- Vue.js中的8种组件间的通信方式;3个组件实例是前6种通信的实例,组件直接复制粘贴即可看到运行结果
- LeetCode 476. 数字的补数(移位 异或^)
- 毕业了,就忘掉导师吧
- android html拦截广告,广告见鬼去!两招让安卓告别网页广告
- C#实现捕获当前屏幕截图(转)
- 全球AI芯片企业排行:英伟达第1,华为第12(七家中国公司入围Top24)
- Clojure的并发(六)Agent可以改进的地方
- win10分屏快捷键无法使用_win10分屏快捷键如何使用
- Go语言编程设计学习Day1:helloworld 变量 常量
- 第二型曲线和曲面积分总结
- 16. FizzBuzz
- 三星 android 4.4 kitkat 刷机包,三星 Galaxy S III LTE(i9305) 刷机包 CM11 KitKat 安卓Android4.4 非官方版...
- mysql 海明距离_mysql根据经纬度求两地距离(示例代码)
- 如何快速记忆C语言运算符?
热门文章
- mysql 两条数据相减_mysql 实现相邻两条数据相减
- 哪个服务器可以玩无限火力,lol无限火力2018开放时间 国服测试服已登录 网友:希望这次不要骗人!...
- app.honeycomb.Shell$HomeActivity failed to start
- 接触到的加密算法MD5、SHA1(转)
- MYSQL5.5.48编译安装
- robot framework -重点记录
- try catch无法捕获 StackOverflowException
- Linq 支持动态字查询集合, 也就是说根据传入的值进行查询。
- 在线开启mysql慢查询
- 今天我的生日,纪念一下