lintcode 中等题:kth-largest-element 第k大元素
题目
第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大元素的更多相关文章
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- LeetCode算法题-Kth Largest Element in a Stream(Java实现)
这是悦乐书的第296次更新,第315篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第164题(顺位题号是703).设计一个类来查找流中第k个最大元素.请注意,它是排序顺序 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- Lintcode: Kth Largest Element 解题报告
Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- silverlight将字符串转化为控件
silverlight的System.Windows.Markup命名空间下,提供了XamlReader.Load()方法可以将字符串转换为控件. 代码 StringBuilder sbGrid = ...
- c#配置log4net步骤
1.引入添加log4net.dll引用 2.建立配置文件Log4Net.config(名字自定义).文件内容参考,输出的文件名称可更改 .运行是要放入到相应bin/debug(release) 目录 ...
- python 获取 mac 地址 的代码
python 获取 mac 地址 的例子,有需要的朋友可以参考下. #!/bin/python import os import re def GetMac(): if os.name == ...
- 亚马逊左侧导航(jquery.menuaim.js)
jquery.menuaim.js 主菜单 <div class="active"> <ul class="dropdown-menu" ...
- 《PHP和MySQL Web开发》精彩的地方收录
1.用SESSION来做的购物车,做成数组,用isbn对应书的数量作为二维数组保存 $new GET传值加入购物车,submit是修改数量,提交后的表单,通过历遍原来的数组,对应isbn修改最新的数量 ...
- PHP安全之register_globals
一.register_globals = Off 和 register_globals = On的区别 register_globals是php.ini里的一个配置,这个配置影响到php如何接收传递过 ...
- Delphi XE5教程12:注释和编译器指示字
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...
- Excel中的宏--VBA的简单例子
第一步:点击录制宏 第二步:填写宏的方法名 第三步:进行一系列的操作之后,关闭宏 第四步:根据自己的需要查看,修改宏 第六步:保存,一般是另存为,后缀名为.xlsm,否则宏语言不能保存. 到此为止恭喜 ...
- jquery trigger伪造a标签的click事件取代window.open方法
$(function() { $('#btnyes').click(function () { $('#ssss').attr("href", "http://www.b ...
- 【转载】Powershell获取世纪互联Office365中所有用户的OWA时区
get-mailbox -resultsize unlimited | Get-MailboxRegionalConfiguration | select Identity,TimeZone | wh ...