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. AR*更新客户地址联系人

    CREATE OR REPLACE PACKAGE BODY cux_ar_party_location_pkg IS g_pkg_name CONSTANT VARCHAR2() := 'CUX_A ...

  2. Storm Kafka与配置和代码集成

    1.目标 - 风暴卡夫卡整合 在本Kafka教程中,我们将学习Storm Kafka Integration的概念.此外,我们将在此Kafka Storm集成教程中讨论Storm架构,Storm Cl ...

  3. XShell上传、下载本地文件到linux服务器

    Python之道发表于程序员八阿哥订阅 1.2K 腾讯云服务器 年付3折起 首次购买云服务器 最低3折起 超高性价比 限时抢购 Xshell很好用,然后有时候想在windows和linux上传或下载某 ...

  4. MapReduce Join关联

    Reduce join 原理 Map端的主要工作:为来自不同表(文件)的key/value对打标签以区别不同来源的记录.然后用连接字段作为key,其余部分和新加的标志作为value,最后进行输出. R ...

  5. 【C++札记】实现C++的string类

    C++有了string类使得操作字符串变得很方便.有关string类,面试过程中也经常问到的就是自己实现一个sring类.下边实现个String类供大家参考: String.h #pragma onc ...

  6. 关于MSVCR100.dll、MSVCR100d.dll、Msvcp100.dll、abort()R6010等故障模块排查及解决方法

    一.常见故障介绍 最近在开发相机项目(项目细节由于公司保密就不介绍了),程序运行5个来月以来首次出现msvcr100.dll故障等问题,于是乎开始了分析之路,按照度娘上的一顿操作,期间也是出现了各种不 ...

  7. php将二维数组转换成我想要的一维数组

    使用方法array_column($array,b,$arr): 参数说明:$array是我们原始的二维数组,B是我们想要的values,c是key 举个栗子:这是我原来的二维数组 这是我需要的一维数 ...

  8. vue生成带logo的二维码

    输入命令行下载 npm install vue_qrcodes <template> <div> <qrcode :url="></qrcode&g ...

  9. Python字符串格式化方式之format

    format方式是在Python3引入了一个新的字符串格式化的方法,并且随后支持了Python2.7.这个新的字符串格式化方法摆脱了%操作符并且使得字符串格式化的语法更规范了.现在时候通过调用字符串对 ...

  10. Jmeter_自带脚本录制

    1.http请求+查看结果树 代理服务器操作步骤 1.创建一个线程组(右键点击“测试计划“---> ”添加“ ---> ”线程组“) 2.创建一个http代理服务器(右键“测试计划”--& ...