做国际网站一般做什么风格,网站权重分析,站长工具seo综合查询全面解析,黑龙江建筑职业技术学院招生网站215数组中的第K个最大元素
题目描述
给定整数数组 nums 和整数 k#xff0c;请返回数组中第 k 个最大的元素。
请注意#xff0c;你需要找的是数组排序后的第 k 个最大的元素#xff0c;而不是第 k 个不同的元素。
你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。…215数组中的第K个最大元素
题目描述
给定整数数组 nums 和整数 k请返回数组中第 k 个最大的元素。
请注意你需要找的是数组排序后的第 k 个最大的元素而不是第 k 个不同的元素。
你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
输入: [3,2,1,5,6,4], k 2
输出: 5示例 2:
输入: [3,2,3,1,2,4,5,5,6], k 4
输出: 4提示
1 k nums.length 105-104 nums[i] 104
方法一小顶堆
class Solution {public int findKthLargest(int[] nums, int k) {PriorityQueueInteger pq new PriorityQueue(); // min heapfor (int val : nums) {pq.add(val);if (pq.size() k) {pq.poll();}}return pq.peek();}
}在这个代码中我们首先创建一个优先队列pq。然后我们遍历数组nums对于每个元素我们将其添加到队列中。如果队列的大小大于k我们就删除队列中的最小元素。最后我们返回队列的头部元素这就是第k个最大的元素。
这个算法的时间复杂度是O(n log k)因为我们需要对每个元素进行堆操作堆操作的时间复杂度是O(log k)。虽然题目要求时间复杂度为O(n)但是这个算法在实际中的性能已经非常好了因为log k通常远小于n。
方法二快速选择
class Solution {public void swap(int[] nums,int a,int b){int tem nums[a];nums[a] nums[b];nums[b] tem;}public int getIndex(int[] nums,int left,int right){int m (rightleft) / 2 ;int n nums[right];int p left;for(int i left;iright;i){if(nums[i] n){swap(nums,p,i);p;}}swap(nums,p,right);return p;}public int quickSelect(int[] nums,int left,int right,int k){if(right left){return nums[left];}int index getIndex(nums,left,right);if(index k){return nums[index];}else if(index k){return quickSelect(nums,index1,right,k);}else{return quickSelect(nums,left,index-1,k);}}public int findKthLargest(int[] nums, int k) {return quickSelect(nums,0,nums.length-1,nums.length - k);}
}快速选择是快速排序的一个优化它在平均情况下的时间复杂度是O(n)。
在这个代码中我们首先使用快速选择算法来找到第k个最大的元素。快速选择算法的基本思想是我们选择一个枢轴元素然后将数组分为两部分一部分是小于枢轴的元素另一部分是大于枢轴的元素。然后我们根据k和枢轴的位置决定是在左边的部分还是右边的部分继续查找。如果k等于枢轴的位置那么枢轴就是我们要找的元素。 文章转载自: http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn