215. Kth Largest Element in an Array找出数组中第k大的值
堆排序做的,没有全部排序,找到第k个就结束
public int findKthLargest(int[] nums, int k) {
int num = 0;
if (nums.length <= 1)
return nums[0];
int heapSize = nums.length;
//1.构建最大堆
int half = (heapSize-2)/2;
for (int i = half;i >= 0;i--)
{
adjust(nums,heapSize,i);
}
while (heapSize > 1)
{
//2.取出最大值
swap(0,heapSize-1,nums);
heapSize--;
num++;
if(num == k)
break;
//3.调整堆
adjust(nums,heapSize,0);
}
return nums[nums.length-k];
}
public void adjust(int[] nums,int heapSize,int index)
{
int left = index*2+1;
int right = index*2+2;
int biggest = index;
if (left < heapSize && nums[left] > nums[biggest])
biggest = left;
//注意这里都是和nums[biggest]比较,别写成index,因为要找出最大的值
if (right < heapSize && nums[right] > nums[biggest])
biggest = right;
if (biggest != index)
{
swap(index,biggest,nums);
adjust(nums,heapSize,biggest);
}
}
public void swap(int a,int b,int[] nums)
{
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
堆排序的算法时间复杂度是:O(n*log k )
下边快排的改进做法,O(n)
利用快速排序的思想,从数组S中随机找出一个元素X,把数组分为两部分Sa和Sb。Sa中的元素大于等于X,Sb中元素小于X。这时有两种情况:
public void findk(int[] nums,int sta,int end,int k)
{
if (sta>end) return;
Random random = new Random();
int p = random.nextInt(end-sta)+sta;
swap(nums,p,sta);
int l = sta;
int r = end;
while (l<r)
{
while (l<r&&nums[r]>=nums[sta])
r--;
while (l<r&&nums[l]<=nums[sta])
l++;
swap(nums,l,r); }
swap(nums,l,sta);
//记录右边数组(包括排序好的数)的大小
int size = end-l+1;
if (size == k) System.out.println(nums[l]);
else if (size<k) findk(nums,sta,l-1,k-size);
else findk(nums,l,end,k);
}
public void swap(int[] nums,int i ,int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
215. Kth Largest Element in an Array找出数组中第k大的值的更多相关文章
- 前端算法题:找出数组中第k大的数字出现多少次
题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 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 so ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- 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 ...
- [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 ...
- 【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 ...
随机推荐
- 使用Docker部署MSSQL
部署MSSQL需要2G内存 1.下载镜像 docker pull microsoft/mssql-server-linux 使用该命令就可以把数据库的docker镜像下载下来. 2.创建并运行容器 d ...
- 区块链学习5:智能合约Smart contract原理及发展历程科普知识
☞ ░ 前往老猿Python博文目录 ░ 一.智能合约的定义 通俗来说,智能合约就是一种在计算机系统上,当一定条件满足的情况下可被自动执行的合约,智能合约体现为一段代码及其运行环境.例如银行信用卡的自 ...
- 第1.2节 Python学习环境的使用
Python的环境安装好以后,可以通过IDLE(Python 3.7 64-bit)进入图形界面使用Python,也可以通过Python 3.7 64-bit进入命令行交互式界面,两者都可以使用,不过 ...
- 第十二章 Python标准库内置模块和包简介
在<第十章 Python的模块和包>老猿详细介绍了Python模块和包的相关概念,模块和包是Python功能扩展的重要手段,也是Python开放的重要特征.为了提供强大的能力,Python ...
- Python正则表达式re.findall("[A-Za-z]([A-Za-z0-9])*[.]txt",'Abc2019.txt')的结果为什么是['9']
在<Python妙用re.sub分析正则表达式匹配过程>中老猿分析了findall函数的返回情况,老猿前一阵子在执行这个语句时: >>> re.findall(" ...
- CTF SHOW WEB_AK赛
CTF SHOW平台的WEB AK赛: 签到_观己 <?php if(isset($_GET['file'])){ $file = $_GET['file']; if(preg_match( ...
- c#如何取出指定的中间文本
///<summary> ///取出文本中间内容 ///<summary> ///<param name="left">左边文本</par ...
- 中间件面试专题:kafka高频面试问题
- 【Codeforces 1083C】Max Mex(线段树 & LCA)
Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...
- 【HNOI2010】城市建设(对时间分治 & Kruskal)
Description \(n\) 个点 \(m\) 条边的带边权无向图.\(q\) 次操作,每次修改一条边的权值. 求每次修改后的最小生成树的边权和. Hint \(1\le n\le 2\time ...