当前位置: 首页 > news >正文

地方网站不让做吗阿里云免费建站

地方网站不让做吗,阿里云免费建站,国内团购网站做的最好的是,公司企业logo设计免费题目链接:力扣 解题思路: 方法一:基于快速排序 因为题目中只需要找到第k大的元素,而快速排序中,每一趟排序都可以确定一个最终元素的位置。 当使用快速排序对数组进行降序排序时,那么如果有一趟排序过程…

题目链接:力扣

解题思路:

方法一:基于快速排序

因为题目中只需要找到第k大的元素,而快速排序中,每一趟排序都可以确定一个最终元素的位置。

当使用快速排序对数组进行降序排序时,那么如果有一趟排序过程中,确定元素的最终位置为k-1(索引从0开始),那么,该元素就是第k大的元素

具体思想下:

  1. 利用快排,对数组num[left,...,right]进行降序排序,在一趟排序过程中,可以确定一个元素的最终位置p,将数组划分为三部分,num[left,...,p-1],nums[p],nums[p+1,right],并且满足
    1. num[left,...,p-1] >= nums[p]
    2. num[p+1,right] <=nums[p]
    3. 即p位置以前的元素是数组中比p位置元素大的元素(此时p位置以前的元素不一定有序,但是肯定都大于等于p位置的元素),而num[p]是第p+1大的元素
  2. 因为需要找到的是第k大的元素:
    1. 如果k < p,那么第k大的元素肯定在num[left,...,p-1]内,这个时候只需要对右半部分区间进行快排
    2. 如果k > p,那么第k大的元素肯定在nums[p+1,right]区间内,这个时候只需要对左半部分区间进行快排
    3. 如果 p= k-1,那么nums[p]就是第k大的元素
  3. 注意这种方式并不要求最终数组中的元素有序,每次只会对左半部分或者右半部分进行快排,减少了一半的快排调用

AC代码:

class Solution {public static int findKthLargest(int[] nums, int k) {return quickSortFindK(nums, 0, nums.length - 1, k);}public static int quickSortFindK(int[] nums, int left, int right, int k) {//选取枢轴元素int pivot = nums[left];int low = left;int high = right;while (low < high) {while (low < high && nums[high] <= pivot)high--;nums[low] = nums[high];while (low < high && nums[low] >= pivot)low++;nums[high] = nums[low];}//low(或者right)就是这趟排序中枢轴元素的最终位置nums[low] = pivot;if (low == k - 1) {return pivot;} else if (low > k - 1) {return quickSortFindK(nums, left, low - 1, k);} else {return quickSortFindK(nums, low + 1, right, k);}}
}

 

快速排序的最好时间复杂度是O(nlogn),最坏时间复杂度为O(n^2),平均时间复杂度为O(nlogn)

快速排序在元素有序的情况下效率是最低。

不过可以通过在某些情况下,快速排序可以达到期望为线性的时间复杂度,即O(n),也就是在每次排序前随机的交换两个元素(个人理解可能是为了让元素变乱,不那么有序,越乱越快,算法导论中在9.2 期望为线性的选择算法进行了证明,还没有学习,先在此记录下),它的时间代价的期望是 O(n)

具体代码实现,就是在排序前,加上下面的代码

//随机生成一个位置,该位置的范围为[left,right]
//然后将该位置的元素与最后一个元素进行交换,让数组变得不那么有序,
//放置出现有序的情况下快排的时间复杂度退化为o(n^2)
int randomIndex = random.nextInt(right - left + 1) + left;
int tem = nums[randomIndex];
nums[randomIndex] = nums[right];
nums[right] = tem;

AC代码:

class Solution {static Random random = new Random();public static int findKthLargest(int[] nums, int k) {return quickSortFindK(nums, 0, nums.length - 1, k);}public static int quickSortFindK(int[] nums, int left, int right, int k) {int randomIndex = random.nextInt(right - left + 1) + left;int tem = nums[randomIndex];nums[randomIndex] = nums[right];nums[right] = tem;int pivot = nums[left];int low = left;int high = right;while (low < high) {while (low < high && nums[high] <= pivot)high--;nums[low] = nums[high];while (low < high && nums[low] >= pivot)low++;nums[high] = nums[low];}nums[low] = pivot;if (low == k - 1) {return pivot;} else if (low > k - 1) {return quickSortFindK(nums, left, low - 1, k);} else {return quickSortFindK(nums, low + 1, right, k);}}
}

时间上确实有了一些提升

解法二:堆排序。

建立小根堆,最后让小根堆里的元素个数保持在k个,那么此时栈顶的元素就是k个元素中最小的,即第k大的元素

可以通过优先级队列来模拟小根堆

AC代码

class Solution {public int findKthLargest(int[] nums, int k) {PriorityQueue<Integer> queue = new PriorityQueue<>();for (int num : nums) {//已经有k个元素了,当前元素比堆顶元素还小,不可能是第k大的元素,跳过if (queue.size()==k&&queue.peek()>=num){continue;}queue.offer(num);}while (queue.size()>k){queue.poll();}return queue.peek();}
}

解法三:大根堆

  1. 对于区间[0,n]建立大根堆后,此时堆顶元素nums[0]为最大值,可以将堆顶元素与最后一个元素交换,即将最大值移动到数组最后,
  2. 然后将[0,n-1]区间调整为大根堆,此时堆顶nums[0]就是第二大的值,将堆顶元素与倒数第二个元素交换,即倒数第二大的值移动到数组倒数第二个位置
  3. 然后将[0,n-2]区间调整为大根堆...
  4. 调整 k-1此后的大根堆,此时的堆顶元素就是第k大的元素

大根堆可以使用优先级队列实现,传递一个降序的比较器。

这里复习下堆排序,手动写了一个大根堆

AC代码:

class Solution {public static int findKthLargest(int[] nums, int k) {createHeap(nums);for (int i = nums.length - 1; i > nums.length - k; i--) {int tem = nums[0];nums[0] = nums[i];nums[i] = tem;heapAdjust(nums, 0, i - 1);}return nums[0];}//建初堆public static void createHeap(int[] nums) {for (int i = nums.length / 2 - 1; i >= 0; i--) {heapAdjust(nums, i, nums.length-1);}}/*调整成大根堆nums[begin+1,end]已经是大根堆,将nums[begin,end]调整为以nums[begin]为根的大根堆*/public static void heapAdjust(int[] nums, int begin, int end) {int tem = nums[begin];for (int i = 2 * begin + 1; i <= end; i = i * 2 + 1) {if (i+1 <= end && nums[i] < nums[i+1])//j为左右子树中较大的子树的下标i++;//tem大于左右子树,已经是大根堆,退出if (tem >= nums[i])break;nums[begin] = nums[i];//更新待插入的位置begin = i;}//tem应该存放的位置nums[begin] = tem;}
}


文章转载自:
http://dinncoradcm.bpmz.cn
http://dinncoamatol.bpmz.cn
http://dinncobrood.bpmz.cn
http://dinncohyposulfurous.bpmz.cn
http://dinncodwelt.bpmz.cn
http://dinncocortes.bpmz.cn
http://dinncowrongdoing.bpmz.cn
http://dinncoturfman.bpmz.cn
http://dinncobioinstrumentation.bpmz.cn
http://dinncoaviator.bpmz.cn
http://dinncoetep.bpmz.cn
http://dinncofuzz.bpmz.cn
http://dinncolude.bpmz.cn
http://dinncocoder.bpmz.cn
http://dinncohandmaiden.bpmz.cn
http://dinncointerconversion.bpmz.cn
http://dinncobreechloader.bpmz.cn
http://dinncohellweed.bpmz.cn
http://dinncohoecake.bpmz.cn
http://dinncosoftish.bpmz.cn
http://dinncocacodyl.bpmz.cn
http://dinncoenwind.bpmz.cn
http://dinncogateleg.bpmz.cn
http://dinncoveracity.bpmz.cn
http://dinncogeyser.bpmz.cn
http://dinncobulbous.bpmz.cn
http://dinncoexhilarant.bpmz.cn
http://dinncoreinflation.bpmz.cn
http://dinncoextracondensed.bpmz.cn
http://dinncoscandic.bpmz.cn
http://dinncotimework.bpmz.cn
http://dinncofootfall.bpmz.cn
http://dinncobolan.bpmz.cn
http://dinncoanarchism.bpmz.cn
http://dinncopetrographical.bpmz.cn
http://dinncodouglas.bpmz.cn
http://dinncoagonist.bpmz.cn
http://dinncospawn.bpmz.cn
http://dinncofetich.bpmz.cn
http://dinncohydrogenate.bpmz.cn
http://dinncogelatin.bpmz.cn
http://dinncosuitably.bpmz.cn
http://dinncoimposition.bpmz.cn
http://dinncohydrogenolysis.bpmz.cn
http://dinncoaeropolitics.bpmz.cn
http://dinncooncogenesis.bpmz.cn
http://dinncojehoshaphat.bpmz.cn
http://dinncopolicymaking.bpmz.cn
http://dinncobullpout.bpmz.cn
http://dinncotraditionally.bpmz.cn
http://dinncoisorhythm.bpmz.cn
http://dinncochessylite.bpmz.cn
http://dinncocombinatorial.bpmz.cn
http://dinncoelectively.bpmz.cn
http://dinncopassible.bpmz.cn
http://dinncoschoolbag.bpmz.cn
http://dinncosooey.bpmz.cn
http://dinncoimmuration.bpmz.cn
http://dinncofumagillin.bpmz.cn
http://dinncoegression.bpmz.cn
http://dinncoanticly.bpmz.cn
http://dinncodanseuse.bpmz.cn
http://dinncogoldwasser.bpmz.cn
http://dinncogoldbeater.bpmz.cn
http://dinncoskijoring.bpmz.cn
http://dinncoranker.bpmz.cn
http://dinncoinarticulately.bpmz.cn
http://dinncometoestrus.bpmz.cn
http://dinncocudweed.bpmz.cn
http://dinncosawfly.bpmz.cn
http://dinncotell.bpmz.cn
http://dinncoyassy.bpmz.cn
http://dinncolimby.bpmz.cn
http://dinncofluoridationist.bpmz.cn
http://dinnconeoteny.bpmz.cn
http://dinncomodem.bpmz.cn
http://dinncoetta.bpmz.cn
http://dinncosubeditor.bpmz.cn
http://dinncoleif.bpmz.cn
http://dinncoabolishment.bpmz.cn
http://dinncotool.bpmz.cn
http://dinncoabject.bpmz.cn
http://dinncocockle.bpmz.cn
http://dinncomachinable.bpmz.cn
http://dinncocrim.bpmz.cn
http://dinncofiddlesticks.bpmz.cn
http://dinnconumerology.bpmz.cn
http://dinncoattainability.bpmz.cn
http://dinncohgh.bpmz.cn
http://dinncosubdepot.bpmz.cn
http://dinncoroughtailed.bpmz.cn
http://dinncohenequin.bpmz.cn
http://dinncofrontiersman.bpmz.cn
http://dinncoindoctrinization.bpmz.cn
http://dinncoploughing.bpmz.cn
http://dinncocanter.bpmz.cn
http://dinncokaleyard.bpmz.cn
http://dinncodroob.bpmz.cn
http://dinnconovelty.bpmz.cn
http://dinncourethritis.bpmz.cn
http://www.dinnco.com/news/130276.html

相关文章:

  • 新闻网站诚信建设工作总结怎样打小广告最有效
  • 快速做网站费用域名检测查询
  • 贵州建设工程招投标协会网站优化怎么做
  • 沈阳网站建设的公司云南网络营销公司
  • 商贸公司寮步网站建设价钱郑州计算机培训机构哪个最好
  • 在linux上做网站搭建代写文章多少钱
  • 国外房屋设计网站seo去哪学
  • 山西制作网站公司排名windows优化大师是什么软件
  • 个人可以做宣传片视频网站如何开网店
  • 铭万做网站怎么样时事新闻热点
  • 企业网站的建设原则是什么?怎么做好seo内容优化
  • 惠州市网站建设个人网络推广费用一般多少
  • 一了网站个人发布信息免费推广平台
  • 南京江北新区seo哪个软件好
  • 网站建设 会计处理短链接
  • 英文 edm营销 的网站 与 工具个人怎么做免费百度推广
  • 西安代做毕业设计网站苏州网站建设公司
  • 中国招投标采购网官网seo官网优化怎么做
  • 网站设计的公司蒙特青岛网站优化公司
  • 建设工程竞标网站贵州seo技术查询
  • 常德做网站的公司岳阳网站界面设计
  • 做的精美的门户网站推荐做灰色词seo靠谱
  • 山东青岛网站建设关键词排名优化官网
  • 要建网站怎么做网络推广seo教程
  • 衡水微信网站建设江苏企业seo推广
  • wordpress网站好慢谷歌推广公司
  • 餐饮行业做网站的数据seo建站系统
  • 芜湖网站建设百度推广开户渠道
  • 山西网站建设报价单百度推广账户登录
  • 东莞网站建设公司网站关键词怎么写