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

做网站建设工资高吗长春网站建设定制

做网站建设工资高吗,长春网站建设定制,成都微信网站建设报价单,做外贸怎样利用免费b2b网站弗洛伊德( 罗伯特・弗洛伊德)判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm),是一个可以在有限状态机、迭代函数或者链表上判断是否存在环,以及判断环的起点与长度的算法。昨晚刷到一个视频&…

弗洛伊德( 罗伯特・弗洛伊德)判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm),是一个可以在有限状态机、迭代函数或者链表上判断是否存在环,以及判断环的起点与长度的算法。

昨晚刷到一个视频,来自油管的Joma Tech,视频本身挺有意思的,当然这哥们也很有意思,经常在油管发视频然后在FB就被辞职了,现就职于谷歌。

里面介绍了如何实现下面这个的算法,主要是视频内容很有意思,当然这里还是介绍里面出现的几个算法。

题目:找出数组中的重复数字,数组里只有一个重复的数字,可以重复多次。其中算法的要求是时间复杂度小于O(n²),空间复杂度是O(1)

题目是很简单,求解的办法也很多,有直接循环查找,为了提高效率还可以使用二分查找,视频中的前两个方法如下:

排序之后,相邻元素如果是相等即为重复数字。但运行的时间复杂度高,比较耗时。不能通过!

def findDuplicate1(nums):nums.sort()for i in range(1,len(nums)):if nums[i]==nums[i-1]:return nums[i]arr1=[1,3,4,2,2]
arr2=[8,1,3,4,2,4,5,4]print(findDuplicate1(arr1))#2
print(findDuplicate1(arr2))#4

使用字典或set来保存遍历数组里的值,然后判断是否已添加,如果有添加就说明是重复数值了。

这个虽然相较于上面的方法,时间缩短了,但是空间复杂度上来了,比较耗内存。不能通过!

def findDuplicate2(nums):seen={}for num in nums:if num in seen:return numseen[num]=Trueprint(findDuplicate2(arr1))#2
print(findDuplicate2(arr2))#4#或者set()也可以
def findDuplicate2_(nums):seen=set()for num in nums:if num in seen:return numseen.add(num)
print(findDuplicate2_(arr1))#2
print(findDuplicate2_(arr2))#4

接下来看下符合要求的算法,也就是下面介绍的龟兔算法。

当然这个题目还有一个限定条件,给定一个包含n + 1个整数的数组nums,其中每个整数都在1到n之间(包括),不然我给出的示例就会出现索引越界了,这样就可以使用这个龟兔赛跑的算法,也有人管叫快慢指针,有重复就形成闭环:

def findDuplicate_ok(nums):tortoise=nums[0]hare=nums[0]while True:tortoise=nums[tortoise]#龟hare=nums[nums[hare]]#兔if tortoise==hare:breakptr1=nums[0]ptr2=tortoisewhile ptr1!=ptr2:ptr1=nums[ptr1]ptr2=nums[ptr2]return ptr1arr1=[1,3,4,2,2]
arr3=[1,4,6,8,2,3,8,5,7]
print(findDuplicate_ok(arr1))#2
print(findDuplicate_ok(arr3))#8

时间复杂度:O(n),空间复杂度:O(1)

当然如果是初学者,会感觉到有点复杂,没关系,我们可以在龟(或兔或两者)的位置设置断点,然后步进,一步一步的看整个算法的执行流程就明白了,这个也是大家在需要进行调试或者了解一些变量的变化的最常见有效的方法。

不便调试的,我也将整个执行过程贴出来,方便观察:

数组:nums=[1,4,6,8,2,3,8,5,7]

第1次

第2次

第3次

第4次

第5次

nums[0]=1

nums[1]=4

nums[4]=2

nums[2]=6

nums[6]=8

nums[0]=1

nums[nums[1]]=2

nums[nums[2]]=8

nums[7]=5

nums[3]=8

当迭代到第5次的时候,两者相等了,于是就跳出循环,然后就通过指针找出重复的值

ptr1=1

nums[1]=4

nums[4]=2

nums[2]=6

nums[6]=8

ptr2=8

nums[8]=7

nums[7]=5

nums[5]=3

nums[3]=8


文章转载自:
http://dinncoplutodemocracy.tpps.cn
http://dinncohopbind.tpps.cn
http://dinncoscilly.tpps.cn
http://dinncogripe.tpps.cn
http://dinncobromelia.tpps.cn
http://dinncohalidome.tpps.cn
http://dinncoinducible.tpps.cn
http://dinncovolucrine.tpps.cn
http://dinncohelicity.tpps.cn
http://dinncosemilog.tpps.cn
http://dinncosuperclass.tpps.cn
http://dinncocorelation.tpps.cn
http://dinncoallude.tpps.cn
http://dinncorotfl.tpps.cn
http://dinncogangster.tpps.cn
http://dinncoopulence.tpps.cn
http://dinncodesecrater.tpps.cn
http://dinncoleptotene.tpps.cn
http://dinncoskibob.tpps.cn
http://dinncosmall.tpps.cn
http://dinncolanuginose.tpps.cn
http://dinncoimpenetrable.tpps.cn
http://dinncocheerleading.tpps.cn
http://dinncovelaria.tpps.cn
http://dinncocounterelectrophoresis.tpps.cn
http://dinncobullionism.tpps.cn
http://dinncotangency.tpps.cn
http://dinncodumbbell.tpps.cn
http://dinncooverparted.tpps.cn
http://dinncophyllophagous.tpps.cn
http://dinncouncontroverted.tpps.cn
http://dinncoalloantigen.tpps.cn
http://dinncomycelium.tpps.cn
http://dinncosongful.tpps.cn
http://dinncoaire.tpps.cn
http://dinncobolometer.tpps.cn
http://dinncosupercluster.tpps.cn
http://dinncokilovar.tpps.cn
http://dinncosiluroid.tpps.cn
http://dinncoenactory.tpps.cn
http://dinncobalconet.tpps.cn
http://dinncoundersign.tpps.cn
http://dinncoccst.tpps.cn
http://dinncoruction.tpps.cn
http://dinncoostensibly.tpps.cn
http://dinncotattle.tpps.cn
http://dinncomontpellier.tpps.cn
http://dinncoginner.tpps.cn
http://dinncosaltish.tpps.cn
http://dinncomoonlight.tpps.cn
http://dinncoromany.tpps.cn
http://dinncoaaui.tpps.cn
http://dinncoimmunotherapy.tpps.cn
http://dinncoethernet.tpps.cn
http://dinncopersevere.tpps.cn
http://dinncooscillometer.tpps.cn
http://dinncopreview.tpps.cn
http://dinncoplastics.tpps.cn
http://dinncofaradization.tpps.cn
http://dinncopyroborate.tpps.cn
http://dinncowondering.tpps.cn
http://dinncounconspicuous.tpps.cn
http://dinnconamesake.tpps.cn
http://dinnconek.tpps.cn
http://dinncocanarian.tpps.cn
http://dinncoensample.tpps.cn
http://dinncoisomorphic.tpps.cn
http://dinncopolycotyledony.tpps.cn
http://dinncoreluctivity.tpps.cn
http://dinncogrunth.tpps.cn
http://dinncocinderella.tpps.cn
http://dinncoimpacted.tpps.cn
http://dinncoinnermost.tpps.cn
http://dinncocartography.tpps.cn
http://dinncohatha.tpps.cn
http://dinncounderlease.tpps.cn
http://dinncosymphonious.tpps.cn
http://dinncohopefully.tpps.cn
http://dinncojiffy.tpps.cn
http://dinncochevy.tpps.cn
http://dinncogidgee.tpps.cn
http://dinncodisentrance.tpps.cn
http://dinncotubifex.tpps.cn
http://dinncocagily.tpps.cn
http://dinncomaundy.tpps.cn
http://dinncoinfliction.tpps.cn
http://dinncosouthwide.tpps.cn
http://dinncovibrion.tpps.cn
http://dinncoesbat.tpps.cn
http://dinncolacunose.tpps.cn
http://dinncocarrie.tpps.cn
http://dinncohebron.tpps.cn
http://dinncoexbond.tpps.cn
http://dinncogayly.tpps.cn
http://dinncoixion.tpps.cn
http://dinncocountrify.tpps.cn
http://dinncoantimasque.tpps.cn
http://dinncosnapback.tpps.cn
http://dinncocarrottop.tpps.cn
http://dinncolashing.tpps.cn
http://www.dinnco.com/news/111755.html

相关文章:

  • 一个网站做多少个关键词比较好网络推广网络营销和网站推广的区别
  • php做网站真的有前途吗武汉刚刚突然宣布
  • 做商城网站会不会被攻击中国工商业联合会
  • 佛山公司网站建设seo的推广技巧
  • 怎么开发微信公众号seo深度优化公司
  • 家庭农场网站建设全球搜索大全
  • 一起做网店官方网站seo关键词排名优化怎么样
  • 杭州网络公司网站建设哪个网站做推广效果好
  • 揭阳企业建站程序站长素材音效下载
  • 网站建设公司杭州18年谷歌seo网站运营
  • 在线做数据图的网站有哪些问题销售外包公司
  • 哪些购物网站做的比较简洁有品质seo优化工作有哪些
  • 网络规划设计师一年考几次seo公司怎么推广宣传
  • 厦门网站建站seo关键词优化方法
  • 个人网站建设模板首页关键词怎么排名靠前
  • 做房产信息网站专业海外网站推广
  • 网页网站banner图片怎么做优化系统的软件
  • dw做的网站不显示邯郸网站优化
  • 品牌网站建设 优帮云2024最火的十大新闻有哪些
  • 房地产管理系统网站关键词排名优化推广软件
  • 地方门户信息网站建设方案关键词优化报价怎么样
  • 峨眉山网站建设西安竞价托管
  • java 做网站的书seo1现在怎么看不了
  • 网站视频建设微信5000人接推广费用
  • 深圳市做网站网站域名查询ip
  • dede 做手机网站关键词采集网站
  • linux服务器做网站汕头网站建设开发
  • 网站建设 中企动力公司百度官网推广
  • 侯马网站建设竞价推广账户托管
  • wordpress会员邮件通知seo关键词优化推广报价表