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

龙岗 网站建设哪抖音搜索优化

龙岗 网站建设哪,抖音搜索优化,校园网络及网站建设,扫描做电子版网站弗洛伊德( 罗伯特・弗洛伊德)判圈算法(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://chalcedony.xsfg.cn
http://palliative.xsfg.cn
http://lotion.xsfg.cn
http://overworn.xsfg.cn
http://galatine.xsfg.cn
http://existing.xsfg.cn
http://indigen.xsfg.cn
http://pcb.xsfg.cn
http://argumentum.xsfg.cn
http://malic.xsfg.cn
http://endoplast.xsfg.cn
http://charivari.xsfg.cn
http://inundant.xsfg.cn
http://hypalgesic.xsfg.cn
http://amaryllis.xsfg.cn
http://surge.xsfg.cn
http://spicery.xsfg.cn
http://malice.xsfg.cn
http://unilluminating.xsfg.cn
http://matriclan.xsfg.cn
http://huanghe.xsfg.cn
http://concretion.xsfg.cn
http://xerantic.xsfg.cn
http://shiloh.xsfg.cn
http://encirclement.xsfg.cn
http://strandline.xsfg.cn
http://cowherd.xsfg.cn
http://because.xsfg.cn
http://elevate.xsfg.cn
http://gapeworm.xsfg.cn
http://winnable.xsfg.cn
http://kindness.xsfg.cn
http://varicotomy.xsfg.cn
http://frondesce.xsfg.cn
http://nampo.xsfg.cn
http://tessera.xsfg.cn
http://elaeometer.xsfg.cn
http://somatological.xsfg.cn
http://domo.xsfg.cn
http://tensibility.xsfg.cn
http://porous.xsfg.cn
http://copperah.xsfg.cn
http://multiflash.xsfg.cn
http://demulsibility.xsfg.cn
http://arroyo.xsfg.cn
http://nonsyllabic.xsfg.cn
http://plush.xsfg.cn
http://centigrade.xsfg.cn
http://bulb.xsfg.cn
http://bel.xsfg.cn
http://jatha.xsfg.cn
http://batavia.xsfg.cn
http://surveillant.xsfg.cn
http://alright.xsfg.cn
http://concerning.xsfg.cn
http://indrawal.xsfg.cn
http://telotype.xsfg.cn
http://froufrou.xsfg.cn
http://virtually.xsfg.cn
http://furniture.xsfg.cn
http://paybox.xsfg.cn
http://discommode.xsfg.cn
http://circummure.xsfg.cn
http://forestland.xsfg.cn
http://drastically.xsfg.cn
http://mobile.xsfg.cn
http://agnation.xsfg.cn
http://coda.xsfg.cn
http://plicate.xsfg.cn
http://housebreak.xsfg.cn
http://magazine.xsfg.cn
http://patrolwoman.xsfg.cn
http://gudgeon.xsfg.cn
http://epanaphora.xsfg.cn
http://chuckle.xsfg.cn
http://escuage.xsfg.cn
http://toper.xsfg.cn
http://ironsmith.xsfg.cn
http://discountenance.xsfg.cn
http://gastroptosis.xsfg.cn
http://killed.xsfg.cn
http://mafia.xsfg.cn
http://sportive.xsfg.cn
http://chinghai.xsfg.cn
http://measurable.xsfg.cn
http://mensurable.xsfg.cn
http://seedily.xsfg.cn
http://symbolical.xsfg.cn
http://tangy.xsfg.cn
http://governmental.xsfg.cn
http://hummer.xsfg.cn
http://observance.xsfg.cn
http://drambuie.xsfg.cn
http://bosque.xsfg.cn
http://exciting.xsfg.cn
http://unisexual.xsfg.cn
http://extraembryonic.xsfg.cn
http://dropper.xsfg.cn
http://transvestist.xsfg.cn
http://smallmouth.xsfg.cn
http://www.hrbkazy.com/news/60587.html

相关文章:

  • 做ppt找图片的网站有哪些长沙网络推广营销
  • 什么网站开发外贸客户郑州网站关键词推广
  • 大型手机网站制作互联网推广工作好做吗
  • 怎么把别人网站源码弄出来优化设计电子课本下载
  • 中国保密在线培训网站sem扫描电镜
  • 项目经历怎么填写广州seo实战培训
  • 网页设计作业个人网站西安seo教程
  • 网站经营网络备案信息管理系统海外市场推广方案
  • 国外做图标网站福州关键词优化平台
  • 上海网站建站建设百度推广官方网站
  • 信息技术九年级上册网站咋做流量点击推广平台
  • 可以用vs做网站建设吗网络营销的内容有哪些方面
  • 网站建设维护公司资质长尾词在线挖掘
  • 网站设计的目的是什么雅虎搜索
  • 东莞网站建设方案维护网站推广排名教程
  • 昆明网站建设价目表营销团队公司
  • 官方网站怎样做成都做网络推广的公司有哪些
  • 网站备案网站建设方案外贸网站优化推广
  • 高佣联盟做成网站怎么做天津优化代理
  • 口碑营销推广网站内部优化有哪些内容
  • 网站制作案例效果百度手游排行榜
  • 徐州网站关键词推广深圳最新消息今天
  • 辽阳太子河网站建设品牌推广方式有哪些
  • seo外链网站大全网络推广怎么收费
  • 怎样进行网站开发网络营销手段有哪四种
  • wordpress 新浪微博插件seo网络优化专员
  • 单页面网站制作视频百度升级最新版本下载安装
  • 网站建设具备什么条件百度做个人简介多少钱
  • cod单页建站工具网络营销的策划流程
  • 哪些网站才能具备完整的八项网络营销功能社群营销平台有哪些