kth-largest-element
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.
思路:
1.利用快速排序partition的思想
2.partition函数返回pivot的下标, 位于pivot左边的数都小于等于pivot,位于pivote右边的数都大于等于pivote.
3.当pivote + 1 == nums.length - k + 1 时,表明 nums[pivote] 为 从大到小的第K个的数
当 pivote + 1 < nums.length - k + 1 时, 表明 第k大的数在pivote 的右边
当 pivote + 1 > nums.length - k + 1 时,表明第K大的书在pivote的左边
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
if (nums == null || nums.length == 0) {
return 0;
}
if (k <= 0) {
return 0;
}
return helper(nums, 0, nums.length - 1, nums.length - k + 1);
}
/*
K 为小于等于第K大的数的个数
*/
public int helper(int[] nums, int l, int r, int k) {
if (l == r) {
return nums[l];
}
int position = partition(nums, l, r);
if (position + 1 == k) {
return nums[position];
} else if (position + 1 < k) {
return helper(nums, position + 1, r, k);
} else {
return helper(nums, l, position - 1, k);
}
}
public int partition(int[] nums, int l, int r) {
if (l == r) {
return l;
}
int left = l, right = r;
int now = nums[left];
while (left < right) {
while (left < right && nums[right] >= now) {
right--;
}
nums[left] = nums[right];
while (left < right && nums[left] <= now) {
left++;
}
nums[right] = nums[left];
}
nums[left] = now;
return left;
}
};
kth-largest-element的更多相关文章
- [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 ...
- 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 ...
- Java for 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 ...
- 【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 ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- Kth Largest Element in an Array - LeetCode
examination questions Find the kth largest element in an unsorted array. Note that it is the kth lar ...
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
- 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 ...
- 【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 ...
- leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...
随机推荐
- Hadoop 系列(五)—— Hadoop 集群环境搭建
一.集群规划 这里搭建一个 3 节点的 Hadoop 集群,其中三台主机均部署 DataNode 和 NodeManager 服务,但只有 hadoop001 上部署 NameNode 和 Resou ...
- 全栈项目|小书架|微信小程序-首页水平轮播实现
首页效果 首页功能主要有 搜索(下篇文章介绍) 图书列表 图书列表 分析一波: 列表是水平滑动 点击列表会有按压效果:布局整体缩小 每个布局的信息从上到下排列分别是:图片.书名.作者.出版社 每个布局 ...
- node-red 使用 创建第一个流程
前言 这只是一个简单的示例,具体详细文档去官网查看 官网指南:https://nodered.org/docs/user-guide/ 打开浏览器,进入编辑器页面:http://localhost:1 ...
- ADO.NET 四(DataReader)
DataReader 类概述 DataReader 类对应MSSQLSERVER在 System.Data.SqlClient 命名空间中,对应的类是 SqlDataReader,主要用于读取表中的查 ...
- 【洛谷 P5357】 【模板】AC自动机(二次加强版)(AC自动机,差分)
每次匹配都不停跳fail显然太慢了,于是在每个节点和fail指向的点连一条边,构成一棵树,在这棵树上差分一下就好了. AC自动机 就这个算法而言其实没用想象中那么难. #include <cst ...
- OO第四单元(UML)单元总结
OO第四单元(UML)单元总结 这是OO课程的第四个单元,也是最后一个单元.这个单元只有两次作业,相比前三个单元少一次作业.而且从内容上讲这个单元的作业目的以了解UML为主,所以相对前三个单元比较简单 ...
- 1+X学习日志——扇形2D效果
section{ width: 500px; height: 300px; border-bottom: 10px solid black; position: relative; margin: 1 ...
- canvas教程(三) 绘制曲线
经过 canvas 教程(二) 绘制直线 我们知道了 canvas 的直线是怎么绘制的 而本次是给大家带来曲线相关的绘制 绘制圆形 在 canvas 中我们可以使用 arc 方法画一个圆 contex ...
- 用c#监控网络流量
using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Runtime.In ...
- navicat for mysql 链接时报错:1251-Client does not support authentication protocol requested by server
客户端使用navicat for mysql.本地安装了mysql 8.0.但是在链接的时候提示: 主要原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 打开mysql命令行输入如下命令 ...