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 ...
随机推荐
- java设计优化--装饰者模式
装饰者模式拥有一个设计非常巧妙的结构,它可以动态的添加功能.在基本的设计原则中,有一条重要的设计准则就是合成/聚合复用原则.根据该原则的思想,代码复用应该尽可能使用委托,而不是使用继承.因为继承是一种 ...
- 在Web Service中傳送Dictionary
有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMethod] public Dictionary< ...
- 基于Bootstrap的jQuery开关按钮插件
按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...
- JavaWeb 项目开发中的技术总结
前言: 在项目开发过程中的一点点指导思想 1.环境准备 win系统 Eclipse 开发平台 maven tomcat Mysql 数据库,mysql5.6 操作数据库的jar 包 JDBC 连接数据 ...
- 最短路算法floyd
内容: 对n个点(n<=450),已知他们的边,也就是相邻关系,求任意两个点的最短距离. 代码: for(int k=1; k<=n; k++)//k写在外面 for(int i=1; i ...
- Python装饰器笔记
DRY(Don't Repeat Yourself)原则: 一般是指在写代码的时候尽量避免重复的实现.违反DRY原则导致的坏处很容易理解,例如维护困难,修改时一旦遗漏就会产生不易察觉的问题. 一.函数 ...
- Syntax error, annotations are only available if source level is 1.5
在项目上右键 -> Properties -> Java Compiler
- android jni
1, java.lang.UnsatisfiedLinkError: Couldn't load xxxxx: findLibrary returned null 当 apk 是被放到 /system ...
- MyEclipse修改项目名称后,部署到 tomcat问题
问题描述: 修改项目名称后,部署到tomcat问题 解决方案: 项目->属性->myelcipse->web下,修 改web context root就可! 要在eclipse里面改 ...
- SSH协议及其应用
SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...