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

重庆网站建设cq网络营销技巧培训班

重庆网站建设cq,网络营销技巧培训班,网站如何做百度推广,前端网站页面模板下载🍎道阻且长,行则将至。🍓 🌻算法,不如说它是一种思考方式🍀算法专栏: 👉🏻123 一、🌱215. 数组中的第K个最大元素 题目描述:给定整数数组nums和整…
🍎道阻且长,行则将至。🍓

🌻算法,不如说它是一种思考方式🍀


算法专栏: 👉🏻123


一、🌱215. 数组中的第K个最大元素

  • 题目描述:给定整数数组nums和整数** k**,请返回数组中第 k 个最大的元素。
    请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。
  • 来源:力扣(LeetCode)
  • 难度:中等
  • 提示:
    1 <= k <= nums.length <= 105
    -104 <= nums[i] <= 104

本题也出现在剑指offer II: 076. 数组中的第 k 大的数字,不过测试案例更友好。

🌴解题

对于本题最常规的解法就是先大到小排序,然后返回第k个元素即可,时间复杂度越低越好。
对于友好的测试案例,也可以使用大小为k的数组进行一次目标变量存储前k大的数。

1.排序法

1.1冒泡排序

最简单的是冒泡排序,方法非常简单,使用两层循环进行逐一遍历,时间复杂度为O(n2)。参考:冒泡排序☝

1.2快速排序

在这个题目要求的==O(n)==时间复杂度,冒泡法是无法通过的,因此考虑快一点的排序算法:快速排序。(大到小为例)
快速排序最直观的理解就是每次选择的key元素(或者基准)经过一趟排序后放在了最终所在的位置,也就是左边大于key元素,右边小于key元素。于是数组被区分为了两个子区间,再继续在两个子区间用同样的方法。这也被称之为分治策略,就是把大的问题变成一个个小的问题,最后组合起来。
例如数组:{4,2,3,5,6,1},对于其中的一次排序:
在这里插入图片描述
我们选取第一个元素为了key,下一个为p,最后一个为q。step1.q向遍历,遇到大于key的元素的位置停下来,step2.p向遍历,遇到小于key的停下来,然后交换pq的元素值。一直重复直到pq相遇,与key交换结束:
在这里插入图片描述
这是其中一种方法,还有挖坑填数、快慢指针:

  • 挖坑填数
    挖坑填数就是在上一个方法的基础上来的,选择的key元素先出来,q指针往左走发现大于key时就把这个元素出来,到上一个坑里,于是新坑出现了;然后p向右走,遇到小于key的也出来,到上一个坑里,一直这样的过程,直到pq相遇,将key最后入这个坑里。(指针是一直向坑遍历

在这里插入图片描述

  • 快慢指针fast、slow
    前面两种方法的指针都是从两端向中间,该方法的指针都是从左至右:都是从key的下一位往右走,快指针每走一步都会判断其指向的元素是否大等于key,若小于则交换两个指针的元素,并且让慢指针移动1位;直到fast遍历完全,slow退回一步(预期的向前了一步),交换slow和key。

在这里插入图片描述

快速排序和冒泡排序解题code:

class Solution076 {public static int findKthLargest(int[] nums, int k) {//排序// bobosort(nums);//快速排序fastsort(nums);return nums[k-1];}private static void fastsort(int[] nums) {int left=0,right= nums.length-1;myquicksort(nums,left,right);}private static void myquicksort(int[] nums, int left, int right) {if(left>right)return;int key=partsort(nums,left,right);myquicksort(nums,left,key-1);myquicksort(nums, key+1, right);}private static int partsort(int[] nums, int left, int right) {int key=left;while(left<right){while(left<right&&nums[right]<=nums[key])right--;while(left<right&&nums[left]>=nums[key])left++;myswap(nums,left,right);}myswap(nums,key,left);return left;}private static void myswap(int[] nums, int left, int right) {int tem;tem=nums[left];nums[left]=nums[right];nums[right]=tem;}public static int[] bobosort(int[] nums){int tem;for (int i = 0; i < nums.length-1; i++) {for (int j = i+1; j < nums.length; j++) {if(nums[i]<nums[j]){tem=nums[i];nums[i]=nums[j];nums[j]=tem;}}}return nums;}}

1.3快速排序思想简化本题

在前面就发现,本题其实找到某个位置之后的一些步骤不需要进行了,例如我们找第8个大的数,在第1次找到第六个元素,那么第8大的元素必然是在后面部分,前面部分数组就可以不管了;当刚好找到第8个时直接返回就是我们的结果。这样可以大大减少搜索时间。

  • code:
class Solution {publicint findKthLargest(int[] nums, int k) {int key=0;int left=0;int right= nums.length - 1;quicksort1(nums,key,left,right,k-1);return nums[k-1];}private static void quicksort1(int[] nums, int key, int left, int right, int k) {if(left>right)return;key = partsort( nums,  key,  left,  right);if(key<k){quicksort1(nums,key+1,key+1,right,k);}else if(key==k){return;}else{quicksort1(nums,left,left,key-1,k);}}private static int partsort(int[] nums, int key, int left, int right) {int slow=key+1;int fast=key+1;int temp;while (fast<=right){if(nums[fast]>=nums[key]){temp=nums[slow];nums[slow]=nums[fast];nums[fast]=temp;slow++;}fast++;}slow--;temp=nums[key];nums[key]=nums[slow];nums[slow]=temp;return slow;}
}

在这里插入图片描述

2.大小为k数组

就是说使用一个大小为k的数组来存储遍历找到前k个最大的数,只需要遍历一遍数组,但是数组k的处理还是需要耗费时间的。

  • code:
class Solution076 {public static int findKthLargest(int[] nums, int k) {int[] numk = new int[k];int j=0;for (int i = 0; i < nums.length; i++) {if(i<k){numk[i]=nums[i];}else{j=findKless(numk);if(numk[j]<nums[i])numk[j]=nums[i];}}j=findKless(numk);return numk[j];}public static int findKless(int[] nums){int k=0;for (int i = 1; i < nums.length; i++) {if(nums[k]>nums[i])k=i;}return k;}
}

该方法只能通过剑指offer的案例:
t215
在这里插入图片描述
剑指offer:
在这里插入图片描述


🌵Bug本是code常态,通过才是稀缺的意外!🌷

在这里插入图片描述

返回第一页☝



☕物有本末,事有终始,知所先后。🍭

🍎☝☝☝☝☝我的CSDN☝☝☝☝☝☝🍓


文章转载自:
http://dinncoravening.bpmz.cn
http://dinncomarduk.bpmz.cn
http://dinncoeconomizer.bpmz.cn
http://dinncodiazotroph.bpmz.cn
http://dinncoreligiose.bpmz.cn
http://dinncoclicker.bpmz.cn
http://dinncoagilely.bpmz.cn
http://dinncopreordain.bpmz.cn
http://dinncozonation.bpmz.cn
http://dinncoincludable.bpmz.cn
http://dinncocochabamba.bpmz.cn
http://dinnconepotistical.bpmz.cn
http://dinncoscurvy.bpmz.cn
http://dinncoprobational.bpmz.cn
http://dinncosciagraph.bpmz.cn
http://dinncoslakeless.bpmz.cn
http://dinncoratch.bpmz.cn
http://dinncoforegrounding.bpmz.cn
http://dinncosulfonic.bpmz.cn
http://dinncoposttensioning.bpmz.cn
http://dinncoyardmaster.bpmz.cn
http://dinncocrocodile.bpmz.cn
http://dinncodyeline.bpmz.cn
http://dinncoludicrously.bpmz.cn
http://dinncophotosynthetic.bpmz.cn
http://dinncotangshan.bpmz.cn
http://dinncopellucidly.bpmz.cn
http://dinncoimperforated.bpmz.cn
http://dinncotundra.bpmz.cn
http://dinncosariwon.bpmz.cn
http://dinncoemerson.bpmz.cn
http://dinncoaerography.bpmz.cn
http://dinncoconductor.bpmz.cn
http://dinncobuttercup.bpmz.cn
http://dinncogalluses.bpmz.cn
http://dinncodisruption.bpmz.cn
http://dinncomeaningly.bpmz.cn
http://dinncosuntanned.bpmz.cn
http://dinncodecreasing.bpmz.cn
http://dinncoarcheologist.bpmz.cn
http://dinncovengeful.bpmz.cn
http://dinncocanaliculated.bpmz.cn
http://dinncoprocurance.bpmz.cn
http://dinncohelipod.bpmz.cn
http://dinncorisible.bpmz.cn
http://dinncomisinterpret.bpmz.cn
http://dinncoaphorism.bpmz.cn
http://dinncojuicehead.bpmz.cn
http://dinncoslip.bpmz.cn
http://dinncohonies.bpmz.cn
http://dinncohempie.bpmz.cn
http://dinncosuspensive.bpmz.cn
http://dinncoisc.bpmz.cn
http://dinncopanocha.bpmz.cn
http://dinncopreggers.bpmz.cn
http://dinncobackstabber.bpmz.cn
http://dinncoclasser.bpmz.cn
http://dinncospiritedness.bpmz.cn
http://dinncofaze.bpmz.cn
http://dinncoisopycnosis.bpmz.cn
http://dinncoquaternion.bpmz.cn
http://dinncoautonym.bpmz.cn
http://dinncopeeling.bpmz.cn
http://dinncocanescent.bpmz.cn
http://dinncocricoid.bpmz.cn
http://dinncolottery.bpmz.cn
http://dinncomuticate.bpmz.cn
http://dinncosuprapersonal.bpmz.cn
http://dinncookenite.bpmz.cn
http://dinnconiftic.bpmz.cn
http://dinncoyerkish.bpmz.cn
http://dinncokeelivine.bpmz.cn
http://dinncovs.bpmz.cn
http://dinncojudea.bpmz.cn
http://dinncoatactic.bpmz.cn
http://dinncodecline.bpmz.cn
http://dinncomonosynaptic.bpmz.cn
http://dinncoimpend.bpmz.cn
http://dinncooverman.bpmz.cn
http://dinncoshiism.bpmz.cn
http://dinncomonofil.bpmz.cn
http://dinncorudesheimer.bpmz.cn
http://dinncomowing.bpmz.cn
http://dinncoforfeiter.bpmz.cn
http://dinncopedophilia.bpmz.cn
http://dinncocontignation.bpmz.cn
http://dinncogardener.bpmz.cn
http://dinncomidwinter.bpmz.cn
http://dinncoirbm.bpmz.cn
http://dinncofortunebook.bpmz.cn
http://dinncocarry.bpmz.cn
http://dinnconapiform.bpmz.cn
http://dinncobewilderingly.bpmz.cn
http://dinncoointment.bpmz.cn
http://dinncoinveterately.bpmz.cn
http://dinncooverbuild.bpmz.cn
http://dinncotandour.bpmz.cn
http://dinncoidol.bpmz.cn
http://dinncophototelescope.bpmz.cn
http://dinncobeachball.bpmz.cn
http://www.dinnco.com/news/115620.html

相关文章:

  • 全国旅游大型网站建设推广形式有哪几种
  • 黄浦专业做网站微信附近人推广引流
  • wordpress标题连接符天津seo代理商
  • 自己的电脑做网站空间视屏品牌推广方案范文
  • 没有服务器怎么先做网站互联网营销师国家职业技能标准
  • 做视频的网站那几个盈利了海南百度推广开户
  • 电商供应链网站贵州seo培训
  • 网络科技公司网站源码腾讯广告推广平台入口
  • 大连网络备案做网站网络营销企业案例分析
  • 如何做授权网站申请域名
  • 网站建立企业中国楼市最新消息
  • 项目网站基础设施建设如何在百度推广自己
  • 中国住房和建设部网站seo建站营销
  • 网站制作群系统seo自然优化排名技巧
  • 网站设计开发制作利尔化学股票
  • python做个人网站最彻底的手机优化软件
  • 个人备案 网站名称 例子免费的短视频app大全
  • 动态网站课程设计百度极速版客服人工在线咨询
  • 东丰在线网站建设宁波seo关键词优化教程
  • 用thinkphp做的网站推广app的单子都在哪里接的
  • 做企业网站需要服务器么百度推广平台登录网址
  • 黄石做网站公司百度云官网首页
  • 网站怎么解析域名解析网站设计平台
  • 寒亭做网站如何做营销推广
  • 黑龙江建设网ca锁网站seo具体怎么做?
  • 网站建设seo 视频淘宝的17种免费推广方法
  • 网站升级中搜索引擎排名优化seo课后题
  • 自己做一网站 多做宣传.搜索引擎优化的主要策略
  • 合肥知名网站制作公司石家庄网络推广平台
  • 西安专业网站建设公司建网站费用