Kth Largest Element in an Array
Find K-th largest element in an array.
Notice
You can swap elements in the array
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的更多相关文章
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- 【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 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- Lettcode Kth Largest Element in an Array
Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 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 ...
随机推荐
- BZOJ-1207 打鼹鼠 DP(LIS)
1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2276 Solved: 1116 [Submit][Statu ...
- 用WinRAR进行安装包的制作
简单的绿色的安装包制作工具,如果不想用复杂且庞大的vs提供的制作工具,或许这个绿色解压安装包是个不错的选择. 下面我收集了一些制作的教程(百度经验的文章)和一些常用到的命令行: WinRAR自解压安装 ...
- Process manufacturing和Discrete manufacturing的区别
Process manufacturing(Process industry) 加工制造,或者加工工业.其一个重要特征是,原材料被加工成成品后,我们再也无法将它恢复成原料,比如,苹果罐头,我们再没法把 ...
- servlet 中 web.xml
<servlet> <servlet-mapping> 他们之间的关系可以使一对一,也可是一对多的关系. <servlet> <servlet-name> ...
- HD2444The Accomodation of Students(并查集判断二分图+匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- SmartImageView&常见的开源代码
1)说明: 该控件实现图片的显示----网络路径也可以显示出来---加载完成之后 就可以 缓存到内存里面!
- 初学Hibernate持久化
hibernate三种持久化对象状态:(持久化对象:Persistent Object=POJO + hbm映射) 1.瞬时状态(临时状态或自由态):PO对象刚创建(即new)开始进入瞬时状态,此时对 ...
- Java初学(七)
一.内部类 1.内部类概述:把类定义在其他类内部,这个类被称为内部类(内部类可以使用static修饰,外部类不可) 2.内部类访问特点:内部类可以直接访问外部类成员,包括私有的 外部类要访问内 ...
- Python序列的切片操作与技巧
切片操作 对于具有序列结构的数据来说,切片操作的方法是:consequence[start_index: end_index: step]. start_index: 表示是第一个元素对象,正索引位置 ...
- Matalab IFS分形算法
IFS 算法代码 function IFS_draw(M,p) N=; :length(p); eval(['a',num2str(k),'=reshape(M(',num2str(k),',:),2 ...