leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array
1 题目
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.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
接口:int findKthLargest(int[] nums, int k)
2 思路
找出数组中第k大的元素。
分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。
3 代码
/**
* 偷懒的做法
*/
public int findKthLargest0(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
/**
* 分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。
*/
public int findKthLargest(int[] nums, int k) {
int len = nums.length;
int goal = len - k;
int left = 0, right = len - 1;
int index = partition(nums, left, right);
while (index != goal) {
if (index < goal) {
left = index + 1;
} else {
right = right - 1;
}
index = partition(nums, left, right);
}
return nums[goal];
}
private int partition(int[] a, int p, int r) {
int i = p - 1;
int x = a[r];
for (int j = p; j < r; j++) {
if (a[j] <= x) {
i++;
swap(a, i, j);
}
}
swap(a, i + 1, r);
return i + 1;
}
private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
4 总结
分治
leetcode面试准备:Kth Largest Element in an Array的更多相关文章
- 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 ...
- 【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 ...
- 【刷题-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- 【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 ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- LeetCode OJ 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 ...
- 【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 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 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 ...
随机推荐
- asp.net 后台弹出提示框
1.后台弹出提示信息方法 Response.Write("<scripttype="text/javascript">alert('你所查询的数据不存在!') ...
- StrHelper
public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...
- 转:jQuery对象与dom对象的转换
jQuery对象与dom对象的转换 发布时间:September 20, 2007 分类:JavaScript <新站上线的手记> <Discuz!多附件上传选择框之jQuery版& ...
- css 利用border属性制作箭头 Using Borders to Make Pure CSS Arrows
不再需要多余的图片 用border属性自然能创造箭头效果 学习地址:http://tech.patientslikeme.com/2010/11/09/using-borders-to-make-pu ...
- 14_Response对象
[简述] Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象. request和response对象既然代表请求和响应 ...
- C语言遍历一个文件夹下面的所有文件
主要用到的函数/function. These should get you started: opendir() readdir() closedir() fopen() fread() fwrit ...
- KMP(匹配)
Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入中含有一些数据, ...
- JSON parser error with double quotes
Use backslash charater \ to escape double quotes in JSON file as below example. { "myInfo" ...
- bzoj 1096: [ZJOI2007]仓库建设
dp是很好想的了,关键是数据太大,普通dp肯定超时,所以一定有用某种优化,dp优化也就那么几种,这道题用的是斜率优化,先写出普通的状态转移方程: dp[i] = min{ dp[j] + Σ ( p ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...