leetcode 215. Kth Largest Element in an Array | Java最短代码实现
2016-03-10 15:31
651 查看
Total Accepted: 46230 Total
Submissions: 143069 Difficulty: Medium
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
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
【抛砖】
该实现是按照快排的思想进行实现,时间复杂度为O(n*logn):
cases passed. Runtime: 55
ms Your runtime beats 27.71% of javasubmissions.
欢迎优化!
Submissions: 143069 Difficulty: Medium
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.
【抛砖】
该实现是按照快排的思想进行实现,时间复杂度为O(n*logn):
public int findKthLargest(int[] nums, int k) { int leftBoundary = 0; int rightBouondary = nums.length - 1; while (leftBoundary < rightBouondary) { int temp = nums[leftBoundary]; int left = leftBoundary; int right = rightBouondary; while (left < right) { while (right > left && nums[right] <= temp) right--; nums[left] = nums[right]; while (left < right && nums[left] > temp) left++; nums[right] = nums[left]; } nums[left] = temp; if (left == k - 1) return nums[left]; else if (left < k - 1) leftBoundary = left + 1; else rightBouondary = left - 1; } return nums[leftBoundary]; }31 / 31 test
cases passed. Runtime: 55
ms Your runtime beats 27.71% of javasubmissions.
欢迎优化!
相关文章推荐
- java反射给实体类赋值
- 为JDK增加新的jar包
- Java多线程基础:进程和线程之由来
- JAVA 异常处理机制
- Spring 注解学习手札(二) 控制层梳理
- java 中的this 关键字的用法
- Spring 中如何向 Bean 注入系统属性或环境变量
- java中equals方法的用法以及==的用法
- Java动态代理
- java 中static 关键字的 用处
- java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment
- Java调用本地接口:java.lang.UnsatisfiedLinkError
- SpringMVC中model的三种返回形式
- Java EE 运用DBHelper连接数据库做一个简单的登录测试
- Struts2中防止表单重复提交的两种方式
- Eclipse中集成SVN(Link方式)
- Java性能分析工具Btrace
- JAVA 时间格式转换
- java垃圾回收机制
- java 压缩zip格式