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.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

思路:

依据时间和空间复杂度,有多种方法。这里给出易于实现,基本能够满足面试、笔试要求的类似于快速排序的(递归)实现。

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
vector<int> leftvec;
vector<int> rightvec; //以nums[0]作为分界元素,从nums[1]开始遍历
//比nums[0]小的元素放入leftvec,反之放入rightvec
for(int i = ; i < nums.size(); i++)
{
if(nums[i] <= nums[])
leftvec.push_back(nums[i]);
else
rightvec.push_back(nums[i]);
} //rightvec的长度即为大于nums[0]的元素的个数
int len = rightvec.size(); if(len >= k)//前k大的元素都在rightvec中,递归求解rightvec
{
return findKthLargest(rightvec, k);
}
else if(len == k - )//比nums[0]大的元素有k-1个,那么nums[0]即为所求结果
{
return nums[];
}
else//比nums[0]大的元素有[0, k-2]个,说明第K大的元素在leftvec中,递归求解leftvec
{
return findKthLargest(leftvec, k - len - );
}
}
};

【LeetCode 215】Kth Largest Element in an Array的更多相关文章

  1. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  2. 【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 so ...

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

  4. 【LeetCode 230】Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  5. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  6. 【刷题-LeetCode】215. Kth Largest Element in an Array

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  7. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

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

  8. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  9. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

随机推荐

  1. Oracle - 位图索引的适用条件

    位图索引的适用条件 位图索引适合只有几个固定值的列,如性别.婚姻状况.行政区等等,而身份证号这种类型不适合用位图索引. 位图索引适合静态数据,而不适合索引频繁更新的列. 举个例子,有这样一个字段bus ...

  2. hdu 3544 Alice's Game

    #include<stdio.h> int main() { int t,n; __int64 sum1,sum2; int i,j,a,b; scanf("%d",& ...

  3. windows下编译FreeSwitch

    FreeSWITCH的是一个跨平台的开源电话交换平台 windows版本:win7 64位的操作系统 [下载] 我下载的是release版本,下载的文件是freeswitch-1.4.20.zip,下 ...

  4. 对象的类型转换P109

    类作为一种应用数据类型,和基本数据类型的变量一样.不同类中存在对象与对象之间的类型转问题,对象的类型转换只能在  具有继承关系的 父类对象-----子类对象 之间进行   子类通常比父类拥有更多的域和 ...

  5. .NET中操作SQLite

    C#操作SQLite Database C#下SQLite操作驱动dll下载:System.Data.SQLite C#使用SQLite步骤: (1)新建一个project (2)添加SQLite操作 ...

  6. 浅析Quartz的集群配置

    浅析Quartz的集群配置(一) 收藏人:Rozdy     2015-01-13 | 阅:1  转:22    |   来源   |  分享               1 基本信息 摘要:Quar ...

  7. Java NIO1

    发现了一个很好的学习Java的外国网站,英语都是很简单的啦,看英语舒服些,关于NIO的系列就直接参照此网站了,而且是英语的! http://tutorials.jenkov.com/ Java NIO ...

  8. NSSize

    #import <Foundation/Foundation.h>   int main(int argc, const char * argv[]) {    @autoreleasep ...

  9. Java API —— TreeSet类

    1.TreeSet类    1)TreeSet类概述         使用元素的自然顺序对元素进行排序         或者根据创建 set 时提供的 Comparator 进行排序          ...

  10. nyoj-257 郁闷的C小加(一) 前缀表达式变后缀

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...