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. socket心跳包机制实践与理解

    实现Socket心跳包主要分为两大类,第一采用tcp自带的KeepAlive,第二是自定义心跳包,恰巧我在产品VICA中都使用过,下面就这两种心跳包机制谈谈个人的理解与感受. 首先第一种KeepAli ...

  2. python的globals()使用

    使用命令pyrasite-shell pid,可以与进程进行shell交互,获取,在shell里执行globals(),可以获取整个进程的全部全局变量,比如django应用.flask应用的变量,而不 ...

  3. 使用SQL Server 扩展事件来创建死锁的跟踪

    我们通过SQL Server 2014图形界面来部署一个扩展事件跟踪会话.然后可以生成SQL脚本. 步骤如下: 步骤1: 通过“对象资源管理器”连接到实例,展开“管理”.“扩展事件”.“会话”. 步骤 ...

  4. 谈谈Ext JS的组件——布局的用法续一

    盒子布局 盒子布局主要作用是以水平(Ext.layout.container.HBox)或垂直方式(Ext.layout.container.VBox)来划分容器区域.这也是比較常有的布局方式. 使用 ...

  5. [转]SQL Server 2005 Integration Services (SSIS) (3) - Business Intelligence Development Studio

    本文转自:http://blog.csdn.net/me_online/article/details/1546281 三,SQL Server Integration Services 开发环境– ...

  6. IDEA java开发 Restful 风格的WebService

    官网:https://www.jetbrains.com/help/idea/restful-webservices.html 1.在IntelliJ中创建新项目,选择Java Enterprise ...

  7. 接口测试框架开发(一):rest-Assured_接口返回数据验证

    转载:http://www.cnblogs.com/lin-123/p/7111034.html 返回的json数据:{"code":"200","m ...

  8. html中锚点的应用【本页面跳转】

    设置锚点 <a name="top"></a> 同页跳转 <a href="#top">返回顶部</a> 不同页 ...

  9. P2P网络借贷系统-核心功能-用户投标-业务解说

    用户投标是P2P网络借贷系统的核心功能.相对照较复杂,为了更好地梳理业务和技术实现思路,特地具体总结分析下. 输入:用户id-uid.标的id-lid.投标金额-amount 1.依据lid,获得贷款 ...

  10. mysql导入数据乱码的解决

    #mysql -uroot -p -hlocalhost --default-character-set=utf8; mysql>use db_name; mysql>source /ho ...