题目

第k大元素

在数组中找到第k大的元素

样例

给出数组[9,3,2,4,8],第三大的元素是4

给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推

注意

你可以交换数组中的元素的位置

挑战

要求时间复杂度为O(n),空间复杂度为O(1)

解题

理论快速排序的思想,每次都减半,这个时间复杂度也是O(N),至于为什么就不知道了

class Solution {
/*
* @param k : description of k
* @param nums : array of nums
* @return: description of return
*/
public int kthLargestElement(int k, int[] nums) {
// write your code here
return quickSort(nums,0,nums.length-1,k); }
public int quickSort(int[] nums,int left,int right,int k){
int i = left;
int j = right;
int tmp = nums[i];
while(i<j){
while(i<j && tmp>=nums[j]) j--;
if(i<j){
nums[i]=nums[j];
i++;
}
while(i<j && tmp<nums[i]) i++;
if(i<j){
nums[j]=nums[i];
j--;
} }
if(i == k -1){
return tmp;
}else if(i< k-1){
return quickSort(nums,i+1,right,k);
}else{
return quickSort(nums,left,i-1,k);
}
}
};

Python

class Solution:
# @param k & A a integer and an array
# @return ans a integer
def kthLargestElement(self, k, A):
return self.quickSort(A,0,len(A)-1,k) def quickSort(self,nums,left,right,k):
i = left
j = right
tmp = nums[i]
while i<j:
while i<j and tmp>=nums[j]:
j -= 1
if i<j:
nums[i] = nums[j]
i += 1
while i<j and tmp< nums[i]:
i += 1
if i<j:
nums[j] = nums[i]
j -= 1
if i == k-1:
return tmp
elif i< k-1:
return self.quickSort(nums,i+1,right,k)
else:
return self.quickSort(nums,left,i-1,k)

Python Code

lintcode 中等题:kth-largest-element 第k大元素的更多相关文章

  1. Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解

    题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...

  2. LeetCode算法题-Kth Largest Element in a Stream(Java实现)

    这是悦乐书的第296次更新,第315篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第164题(顺位题号是703).设计一个类来查找流中第k个最大元素.请注意,它是排序顺序 ...

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

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

  4. Lintcode: Kth Largest Element 解题报告

    Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...

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

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

  6. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. [Swift]LeetCode215. 数组中的第K个最大元素 | 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 ...

  8. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  9. [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  10. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

随机推荐

  1. WIN10主动推升级,有点意思

    不论正与盗,皆推升级,是否一样可用?

  2. 将double类型的值保留几位小数

    1.第一个参数(3.1415926)是要处理的数值.第二个参数(1)为要保留的几位小数.第三个参数是按照“四舍五入”还是"直接取这一位的值"(MidpointRounding.To ...

  3. [大牛翻译系列]Hadoop(13)MapReduce 性能调优:优化洗牌(shuffle)和排序阶段

    6.4.3 优化洗牌(shuffle)和排序阶段 洗牌和排序阶段都很耗费资源.洗牌需要在map和reduce任务之间传输数据,会导致过大的网络消耗.排序和合并操作的消耗也是很显著的.这一节将介绍一系列 ...

  4. Selenium 入门

    我本身也是一个初学者,就顺手记录一下自己的成长记录,共勉 1, 登录selenium官方网站,download 相关的插件. http://docs.seleniumhq.org/ 我用的是eclip ...

  5. c位段

    假如程序表示四盏灯的开关状态灯只有开或关两种状态所以用1和0就可以表示为了节省内存就用一个二进制位表示一盏灯这里就定义位域用 a b c d 各表示一盏 这里定义时注意选用无符号类型位域允许用各种格式 ...

  6. SQL Server 读取CSV中的数据

    测试: Script: create table #Test ( Name ), Age int, T ) ) BULK INSERT #Test From 'I:\AAA.csv' with( fi ...

  7. vs2010的11个调试技巧和方法

    调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...

  8. Ubuntu首次开启root用户

    最近一直在学习linux,选择ubuntu作为联系的操作系统.然后一直发现自己所创建的用户和root用户不是一个概念,执行好多命令的时候都提示没有权限.这样,最后终于发现原来是ubuntu是默认关闭r ...

  9. spring中Bean的注入类型

    1.属性注入    即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际应用中最常采用的注入方式.    属性注入要求Bean提供 ...

  10. Python实战(1)

    此次实战完全按照Python教程 - 廖雪峰的官方网站进行 首先下载windows版本的Python2.7,附上下载链接http://www.python.org/ftp/python/2.7.6/p ...