LeetCode.215 Kth Largest Element in an Array
来源:互联网 发布:淘宝怎么搜索旺旺号 编辑:程序博客网 时间:2024/06/07 00:08
题目:
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.
class Solution { public int findKthLargest(int[] nums, int k) { //给定数组,超出第k大的数 //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了 //PriorityQueue是一个基于优先级堆,此队列的头相对于指定的排序是最小的元素。 //一个具有指定的初始容量。并且按自然排序。 PriorityQueue<Integer> largeK=new PriorityQueue<Integer>(); for(int n:nums){ if(largeK.size()<k){ largeK.add(n); }else{ //集合中已经存在k个元素 //判断是否小于最小值 if(n>largeK.peek()){ largeK.poll(); largeK.add(n); } } } //最小的就是第k个结果 return largeK.poll(); }}
分析2(简介版PriorityQueue):
class Solution { public int findKthLargest(int[] nums, int k) { //给定数组,超出第k大的数 //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了 //PriorityQueue是一个基于优先级堆,此队列的头相对于指定的排序是最小的元素。 //一个具有指定的初始容量。并且按自然排序。设置k+1方便当size大于k时,剔除最前面的。 PriorityQueue<Integer> largeK=new PriorityQueue<Integer>(k+1); for(int n:nums){ //添加元素 largeK.add(n); //如果长度大于k,则提出最小的,也就是最前面的 if(largeK.size()>k){ largeK.poll(); } } //最小的就是第k个结果 return largeK.poll(); }}
分析3(List实现-时间复杂度较高):
class Solution { public int findKthLargest(int[] nums, int k) { //给定数组,超出第k大的数 //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了 List<Integer> list=new ArrayList<Integer>(); for(int i=0;i<nums.length;i++){ if(list.size()<k){ list.add(nums[i]); }else{ //已经存在k个元素 int min=0; //找出集合中最小的下标,和当前元素进行类比 for(int j=1;j<list.size();j++){ if(list.get(j)<list.get(min)){ min=j; } } //类比 if(nums[i]>list.get(min)){ //在最小值位置替换 list.set(min,nums[i]); } } } int res=0; for(int i=1;i<list.size();i++){ if(list.get(i)<list.get(res)){ res=i; } } return list.get(res); }}
分析4(数组-排序实现,时间复杂度最高):
class Solution { public int findKthLargest(int[] nums, int k) { //给定数组,超出第k大的数 //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了 //方法3:使用数组实现,每次都对数组进行排序 int [] array=new int[k]; for(int i=0;i<nums.length;i++){ if(i<k){ array[i]=nums[i]; }else{ //对数组排序 Arrays.sort(array); if(nums[i]>array[0]){ array[0]=nums[i]; } } } Arrays.sort(array); return array[0]; }}
阅读全文
0 0
- Leetcode Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- [LeetCode] Kth Largest Element in an Array
- [leetcode] Kth Largest Element in an Array
- leetcode--Kth Largest Element in an Array
- #leetcode#Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- 【Leetcode】Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- Kth Largest Element in an Array -- leetcode
- [Leetcode]Kth Largest Element in an Array
- [LeetCode]Kth Largest Element in an Array
- *LeetCode-Kth Largest Element in an Array
- LeetCode----Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- leetcode-Kth Largest Element in an Array
- LeetCode -- Kth Largest Element in an Array
- js中通过split函数分割字符串成数组
- FZU
- 集装箱数据众包平台中期检查个人总结
- R和RStudio软件安装
- 大数据处理框架的类型、比较和选择
- LeetCode.215 Kth Largest Element in an Array
- 二叉树的遍历
- 面向大数据的分布式调度
- Qt源码编译configure配置参数
- 了解MES
- HDOJ1018 Big Number(斯特林公式求阶乘位数)
- Python语言入门(四)字典与Set集合用法体验
- 深度学习2:为什么深度学习要使用numpy库
- leetcode011-Best Time to Buy and Sell Stock with Transaction Fee