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的更多相关文章

  1. [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 ...

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

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

  4. 【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 ...

  5. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

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

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

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

  9. 【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 ...

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

随机推荐

  1. NEST health与settings

    /// <summary> /// 创建Idx,并设置分片和副本 /// </summary> public void Settings() { var response = ...

  2. Python接口自动化基础---cookie绕过登录

    使用fiddler获取登录cookie 对比登录前和登录后的cookis 登录前 登录后: 获得cookie之后,使用cookie访问,就可以获取登录态: import requests url='h ...

  3. CLASSPATH 环境变量设置

    当 JVM 运行时,如果设置了 CLASSPATH 变量,那么 JVM 会到该目录下寻找 java 类文件 该变量配置的方式不同,寻找顺序也不同 在我的 D:\tmp\java\quickstart\ ...

  4. Jmeter学习笔记(十一)——定时器

    默认情况下,Jmeter线程在发送请求之间没有间歇.不设置定时器,短时间内会产生大量访问请求,导致服务器被请求淹没,利用Jmeter进行压测时,一般会和定时器一起,控制请求的吞吐量和并发数. 一.定时 ...

  5. c# Unicode

  6. python接口自动化13-data和json参数傻傻分不清

    前言 在发post请求的时候,有时候body部分要传data参数,有时候body部分又要传json参数,那么问题来了:到底什么时候该传json,什么时候该传data? 一.识别json参数 1.在前面 ...

  7. 191012 python3关于空格打印、赋值、+=符号的小坑

    1. python3中,直接打印空格不显示,不论是pycharm,cmd命令窗口,还是linux中,都有尝试,但是空格' '不能显示出来: # 打印菱形,只能用center方法for i in ran ...

  8. vue中8种组件通信方式, 值得收藏!

    vue是数据驱动视图更新的框架, 所以对于vue来说组件间的数据通信非常重要,那么组件之间如何进行数据通信的呢? 首先我们需要知道在vue中组件之间存在什么样的关系, 才更容易理解他们的通信方式, 就 ...

  9. C++(三十八) — 继承方式、访问控制、构造和析构、虚继承

    派生类继承了基类的所有成员,但不包含 构造函数.析构函数.默认赋值运算符.  1.继承方式.访问控制 (1)protected属性:类的对象不能访问该属性成员,但派生类的成员函数可以访问基类的prot ...

  10. mysqlslap压测

    mysqlslap 是MySQL自带的压测工具: -P --create-schema=test -S /tmp/mysql_sandbox18601.sock --number-of-queries ...