Find K-th largest element in an array.

Notice

You can swap elements in the array

Example

In array [9,3,2,4,8], the 3rd largest element is 4.

In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.

分析:

使用partion把array分成两组,然后看中间那个数在哪个位置。然后再确定是继续在左半部分找还是在右半部分找。

 public class Solution {
public int findKthLargest(int[] nums, int k) {
if (nums == null || nums.length == || k > nums.length) return -;
int start = , end = nums.length - ;
while (start <= end) {
int p = partition(nums, start, end);
if (p == nums.length - k) {
return nums[p];
} else if (p < nums.length - k) {
start = p + ;
} else {
end = p - ;
}
}
return -;
} private int partition(int[] nums, int start, int end) {
int p = start;
for (int i = start; i <= end - ; i++) {
if (nums[i] < nums[end]) {
swap(nums, p, i);
p++;
}
}
swap(nums, p, end);
return p;
} private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
 public void print3largest(int[] arr) {
if (arr == null || arr.length < ) {
System.out.print(" Invalid Input ");
return;
} int i, first, second, third; third = first = second = Integer.MIN_VALUE;
for (i = ; i < arr.length; i++) {
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
} else if (arr[i] > second) {
third = second;
second = arr[i];
} else if (arr[i] > third) {
third = arr[i];
}
} System.out.println("Three largest elements are " + first + " " + second + " " + third);
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Kth Largest Element in an Array的更多相关文章

  1. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

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

  3. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  4. Leetcode 之 Kth Largest Element in an Array

    636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...

  5. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  6. Lettcode Kth Largest Element in an Array

    Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...

  7. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

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

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

随机推荐

  1. Ibatis学习总结5--动态 Mapped Statement

    直接使用 JDBC 一个非常普遍的问题是动态 SQL.使用参数值.参数本身和数据列都 是动态的 SQL,通常非常困难.典型的解决方法是,使用一系列 if-else 条件语句和一连串 讨厌的字符串连接. ...

  2. java连接sql server2000/2005

    接触Java或者JSP,难免会使用到数据库SQL Server 2000/2005(我使用2005标准版[9.0.3054]测试),经过自己的搜索和研究,使用JDBC连接SQL Server成功,特此 ...

  3. 基于Bootstrap的jQuery开关按钮插件

        按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...

  4. Quartz 定时任务管理

    前言 将项目中的所有定时任务都统一管理吧,使用 quartz 定时任务 设计思路 使用 quartz 的相关jar 包,懒得去升级了,我使用的是 quart 1.6 写一个定时任务管理类 用一张数据库 ...

  5. 在Linux中怎么把用户添加到组中

    (1)添加用户test,初始密码123456,该用户的主目录为/home/share,用户的基本组为root,用户的shell为/bin/tcsh,要求将该用户加到mail和new组中.请问该怎么做啊 ...

  6. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)

    题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...

  7. Windows下python的配置

    Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...

  8. BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue

    闻说HNOI每年都有一道Hash. 1206: [HNOI2005]虚拟内存 Time Limit: 50 Sec Memory Limit: 162 MB Submit: 330 Solved: 2 ...

  9. POJ3070Fibonacci(矩阵快速幂+高效)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11587   Accepted: 8229 Descri ...

  10. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...