Given an integer array, find the top k largest numbers in it.

 
Example

Given [3,10,1000,-99,4,100] and k = 3.
Return [1000, 100, 10].

解法一:

 class Solution {
public:
/*
* @param nums: an integer array
* @param k: An integer
* @return: the top k largest numbers in array
*/
vector<int> topk(vector<int> nums, int k) {
std::priority_queue<int> heap;
for(int i = ; i < nums.size(); i++) {
heap.push(nums[i]);
} std::vector<int> result;
for (int i = ; i < k; i++) {
result.push_back(heap.top());
heap.pop();
} return result;
}
};

优先队列,类似于维护一个大小为k的最大堆/最小堆(STL中的priority_queue默认是最大堆),时间复杂度为O(n * logk)

解法二:

 class Solution {
/*
* @param nums an integer array
* @param k an integer
* @return the top k largest numbers in array
*/
public int[] topk(int[] nums, int k) {
int[] temp = new int[k];
if(nums == null || nums.length == 0) {
return temp;
}
quickSort(nums, 0, nums.length - 1, k); if(nums.length < k) {
return nums;
} for(int i = 0; i < k; i++) {
temp[i] = nums[i];
} return temp;
} public void quickSort(int[] nums, int start, int end, int k) {
if (start >= end) {
return;
} int left = start, right = end;
int mid = left + (right - left) / 2;
int pivot = nums[mid]; while(left <= right) {
while(left <= right && nums[left] > pivot) {
left++;
} while(left <= right && nums[right] < pivot) {
right--;
} if(left <= right) {
swap(nums, left, right);
left++;
right--;
}
} quickSort(nums, start, right, k);
quickSort(nums, left, end, k);
} private void swap(int[] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
};

快速排序,取出前k大的数,时间复杂度O(n*logn + k)

544. Top k Largest Numbers【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. Top k Largest Numbers

    Given an integer array, find the top k largest numbers in it. Example Given [3,10,1000,-99,4,100] an ...

  3. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  4. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  5. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  6. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  7. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

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

  9. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

随机推荐

  1. SQL盲注测试高级技巧

    写在前面: 这篇文章主要写了一些加快盲注速度的技巧和盲注中比较精巧的语句,虽然注入并不是什么新技术了.但是数据库注入漏洞依然困扰着每一个安全厂商,也鞭策着每一个安全从业者不断前进. 正文: 首先来简单 ...

  2. jquery 获取父窗口的元素、父窗口、子窗口

    一.获取父窗口元素: $("#父窗口元素ID",window.parent.document):对应javascript版本为window.parent.document.getE ...

  3. (转)NIO 文件锁定

    文件锁定 概述 文件锁定初看起来可能让人迷惑.它 似乎 指的是防止程序或者用户访问特定文件.事实上,文件锁就像常规的 Java 对象锁 ― 它们是 劝告式的(advisory) 锁.它们不阻止任何形式 ...

  4. intelliJ idea提示api注释

  5. 如何使用angularjs实现表单验证

    <!DOCTYPE html> <html ng-app="myApp"> <head> <title>angularjs-vali ...

  6. VS2013编译boost1.55库

    1. 官网下载最新的Boost库,我的是1.55 2. 在使用vs2013编译boost-1.55.0之前,先要给boost做下修改: boost_1_55_0\boost\intrusive\det ...

  7. gcc编译选项汇集

    gcc -g 调试选项(DEBUGGING OPTION)GNU CC拥有许多特别选项,既可以调试用户的程序,也可以对GCC排错: -g 以操作系统的本地格式(stabs, COFF, XCOFF,或 ...

  8. php文件上传接口及文件上传错误服务器配置

    一:上传表单 <form enctype="multipart/form-data" action="doFileUp.php" method=" ...

  9. [iOS开发] 使用Jenkins自动打包并上传至蒲公英

    设置构建触发器 Poll SCM H/2 * * * * 设置 构建脚本 # #xodebuild & jenkins 自动构建并上传至pgyer.com #2017年5月9日 # #定义一些 ...

  10. HBase - 计数器 - 计数器的介绍以及使用 | 那伊抹微笑

    博文作者:那伊抹微笑 csdn 博客地址:http://blog.csdn.net/u012185296 itdog8 地址链接 : http://www.itdog8.com/thread-215- ...