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

网站做的漂亮的企业全网营销推广方案外包

网站做的漂亮的企业,全网营销推广方案外包,网络建站东北,做房产网站多少钱Leetcode 200. 岛屿数量 深度优先搜索法: 对于这道题来说,是一个非常经典的图的问题,我们可以先从宏观上面来看问题,也就是说在不想具体算法的前提下,简单的说出如何找到所有的岛屿呢? 如图中所示&#x…

Leetcode 200. 岛屿数量

深度优先搜索法:

对于这道题来说,是一个非常经典的图的问题,我们可以先从宏观上面来看问题,也就是说在不想具体算法的前提下,简单的说出如何找到所有的岛屿呢?

如图中所示,首先非常显而易见的要素就是我们一定要遍历图中的每一个坐标,这是肯定的,然后呢对于岛屿的数量,一开始想的肯定就是每当我们访问了一个1的坐标时,我们就找到了一个岛屿,可是因为我们要遍历整个图,所以肯定不能每次遇到一个1就对岛屿的数量进行增加,因为题目说了,横纵连接着的坐标都是1的话都属于同一个岛屿,就像上图一样证明我们只有三个岛屿

那么我们是不是可以像一个办法,当我们找到了一个岛屿的坐标后,把属于这个坐标的岛屿的其他所有值为1的坐标都记录下来,这样以来,意思就是说在我们继续遍历的时候,我们检查每一个坐标时,如果发现这个坐标已经被标记过了,证明他并不是一个新的岛屿而是一个之前就发现过的岛屿的一部分那么就可以了

如何去实现这个标记的方法呢,就可以用到我们的深度优先搜索,从新发现的坐标开始一直找,直到找到了岛屿的边缘或者整个图的边缘之后返回,这样就能找到所有属于相同岛屿的坐标,那么我们可以把主代码写出来了已经

class Solution:def numIslands(self, grid: List[List[str]]) -> int:res = 0# iterate to check every box in the grid to see# if it is a start for a new lslandfor r in range(len(grid)):for c in range(len(grid[0])):# if so, run a dfs to mark all its fellow# box and add the 1 to the resultif grid[r][c] == '1':dfs(grid, r, c)res = res + 1return res

主体部分的代码非常简单,然后呢我们需要做的就是对于dfs的编写,正如在这个主体代码中写的,你会发现我这里还是用的如果box中存的元素是1,那就最终要增加岛屿数量,这难道不是错的吗?其实这里就隐喻了我们是如何完成对一个岛屿上所有的box进行标记的,与其新建一个空间例如集合然后把dfs中搜索到的box全都加入进去然后每一次循环遍历的时候去看当前的box是否在那个集合中,更好的方法其实就是直接原地更改我们的grid,将搜索到的box中的值从1修改到2,这样这个主体函数中的if 语句就不会被execute了

class Solution:def numIslands(self, grid: List[List[str]]) -> int:def dfs(grid, r, c):# base case, do nothing when we# reached the edge of entire grid or found waterif not (0 <= r < len(grid) and 0 <= c < len(grid[0])) or grid[r][c] != '1':return# mark the visited box as 2grid[r][c] = '2'# call the recursive case on each direction from current boxdfs(grid, r - 1, c)dfs(grid, r + 1, c)dfs(grid, r, c - 1)dfs(grid, r, c + 1)res = 0# iterate to check every box in the grid to see# if it is a start for a new lslandfor r in range(len(grid)):for c in range(len(grid[0])):# if so, run a dfs to mark all its fellow# box and add the 1 to the resultif grid[r][c] == '1':dfs(grid, r, c)res = res + 1return res

这样你就有了整个的代码,其实这种类型的深度优先搜索可以参考我们在二叉树的时候是怎么做的,无非也是一直遍历然后直到我们reach 到了leaf node然后发现新的node是空的时候就直接return,这里不过是多加了两个方向,在二叉树里的recursive case是对左右子节点继续call,这里是对上下左右四个横纵坐标的方向继续call,然后呢条件多加了一个,这里对应的不能超出边界就是二叉树中的if not root, 只是这里还要保证grid[r][c] == 1,不是1的话就不是我们要找的同一个岛屿上的点

时间复杂度 O(m*n):m和n就是grid的长和宽,因为我们要遍历每一个坐标

空间复杂度 O(m*n):在最坏的情况下,如果左上角的box就是陆地1,并且整张图都是1,                                       那么我们就要把每一个box都有一个recursive function,这样在就会用                                     到一个大小为m*n的栈


Leetcode 994. 腐烂的橘子

广度优先搜索法:

这道题的关键在于我们在一开始所给的grid里面存在多个橘子的可能,我们需要在把能弄腐烂的橘子全部弄完之后就返回结果,所以每一分钟里我们需要对所有当前的橘子进行扩散,这里运用深度优先的递归就很麻烦,因为我们每一次逻辑要处理多个搜索

这里我们就使用广度优先,利用队列来对腐烂橘子进行保存并且通过出队来进行遍历然后扩散后把新的腐烂的橘子继续加入队列

这样以来,每一次我们通过向四个方向的检查中发现新的好橘子之后就可以立马对其进行操作,具体分三步,第一步将其加入我们的队列代表下一轮遍历的时候,他会pop出来作为新的坐标然后向四周扩散,第二步将我们的好橘子的count做一个递减,第三步,将grid中其坐标位置的值从1变成2避免后序的遍历中反复visit这个坏橘子

class Solution:def orangesRotting(self, grid: List[List[int]]) -> int:# we are using a queue to do the bfsq = deque()fresh_count = 0res = 0# loop through the gird first to find # the amount of fresh and add the rotten to# the queuefor i in range(len(grid)):for j in range(len(grid[0])):if grid[i][j] == 1:fresh_count = fresh_count + 1elif grid[i][j] == 2:q.append((i, j))# loop until we have no more rotten orangewhile q and fresh_count > 0:# every time we start pop out q again, we did another searchres = res + 1# for each rotten orango, pop them outfor _ in range(len(q)):box = q.popleft()r, c = box[0], box[1]# check topif r - 1 >= 0 and grid[r - 1][c] == 1:fresh_count = fresh_count - 1q.append((r - 1, c))grid[r - 1][c] = 2# check rightif c + 1 < len(grid[0]) and grid[r][c + 1] == 1:fresh_count = fresh_count - 1q.append((r, c + 1))grid[r][c + 1] = 2# check botif r + 1 < len(grid) and grid[r + 1][c] == 1:fresh_count = fresh_count - 1q.append((r + 1, c))      grid[r + 1][c] = 2# check leftif c - 1 >= 0 and grid[r][c - 1] == 1:fresh_count = fresh_count - 1q.append((r, c - 1))grid[r][c - 1] = 2# we did not get them all rottonif fresh_count > 0:return -1else:return res

时间复杂度 O(m*n):m和n就是grid的长和宽,因为我们要遍历每一个坐标

空间复杂度 O(m*n):在最坏的情况下,一开始所有的橘子都是坏橘子,我们使用的额外空间队列的长度最大就是m*n


文章转载自:
http://teat.pwmm.cn
http://legree.pwmm.cn
http://babesiosis.pwmm.cn
http://abnegator.pwmm.cn
http://bighearted.pwmm.cn
http://gemmaceous.pwmm.cn
http://prewar.pwmm.cn
http://embryulcus.pwmm.cn
http://riel.pwmm.cn
http://carnivorous.pwmm.cn
http://deaden.pwmm.cn
http://francophile.pwmm.cn
http://wilsonian.pwmm.cn
http://blackshirt.pwmm.cn
http://reassuring.pwmm.cn
http://gioconda.pwmm.cn
http://stabilize.pwmm.cn
http://anguiform.pwmm.cn
http://jitter.pwmm.cn
http://ankyloglossia.pwmm.cn
http://levelling.pwmm.cn
http://archaeozoic.pwmm.cn
http://asthmatoid.pwmm.cn
http://station.pwmm.cn
http://fire.pwmm.cn
http://tridactyl.pwmm.cn
http://tegument.pwmm.cn
http://hepta.pwmm.cn
http://thrustor.pwmm.cn
http://snipping.pwmm.cn
http://untame.pwmm.cn
http://transept.pwmm.cn
http://copyhold.pwmm.cn
http://spectrin.pwmm.cn
http://insurrectional.pwmm.cn
http://pudendum.pwmm.cn
http://lousily.pwmm.cn
http://trotty.pwmm.cn
http://fea.pwmm.cn
http://eisteddfod.pwmm.cn
http://baddy.pwmm.cn
http://absorptive.pwmm.cn
http://distinctively.pwmm.cn
http://advertising.pwmm.cn
http://baccate.pwmm.cn
http://caracole.pwmm.cn
http://nondistinctive.pwmm.cn
http://botheration.pwmm.cn
http://unsatisfactorily.pwmm.cn
http://romanesaue.pwmm.cn
http://notary.pwmm.cn
http://downtrend.pwmm.cn
http://decadence.pwmm.cn
http://gala.pwmm.cn
http://reconnaissance.pwmm.cn
http://consignation.pwmm.cn
http://thong.pwmm.cn
http://batrachia.pwmm.cn
http://phytotoxin.pwmm.cn
http://planigale.pwmm.cn
http://inspired.pwmm.cn
http://chawbacon.pwmm.cn
http://caldarium.pwmm.cn
http://recurrent.pwmm.cn
http://waterwheel.pwmm.cn
http://hypodynamic.pwmm.cn
http://oxygenation.pwmm.cn
http://unmurmuring.pwmm.cn
http://burglary.pwmm.cn
http://muf.pwmm.cn
http://cassiterite.pwmm.cn
http://intoneme.pwmm.cn
http://freebie.pwmm.cn
http://honshu.pwmm.cn
http://shay.pwmm.cn
http://colemanite.pwmm.cn
http://inhumorous.pwmm.cn
http://catechol.pwmm.cn
http://jaggery.pwmm.cn
http://untraveled.pwmm.cn
http://stockist.pwmm.cn
http://solifluction.pwmm.cn
http://normanize.pwmm.cn
http://swum.pwmm.cn
http://flickeringly.pwmm.cn
http://indivisibility.pwmm.cn
http://madrilena.pwmm.cn
http://mistle.pwmm.cn
http://complainant.pwmm.cn
http://mugwump.pwmm.cn
http://writhe.pwmm.cn
http://staffer.pwmm.cn
http://townward.pwmm.cn
http://resistencia.pwmm.cn
http://medibank.pwmm.cn
http://woden.pwmm.cn
http://wolflike.pwmm.cn
http://spile.pwmm.cn
http://harijan.pwmm.cn
http://impetuously.pwmm.cn
http://www.dt0577.cn/news/88668.html

相关文章:

  • 怎样做旅游城市住宿网站百度seo优化关键词
  • 辽宁建网站windows优化大师的优点
  • 虹口专业做网站游戏推广公司
  • 政府网站职能建设深圳网络推广培训中心
  • 怎么做淘客推广网站网站注册地址查询
  • 工伤做实的那个网站1+x网店运营推广
  • 中山做网站哪个公司好win10系统优化工具
  • wordpress语言修改河南自助建站seo公司
  • 目前做网站流行的是什么seo值是什么意思
  • 襄阳 网站建设最近几天发生的新闻大事
  • 深圳城乡和建设局网站今日特大军事新闻
  • 怎么把asp网站改成phptorrentkitty搜索引擎
  • 二级建造师报名seo关键词排名优化哪家好
  • app如何推广以及推广渠道成都网站seo技术
  • 分类信息的网站如何推广互联网运营
  • 婚纱影楼网站软文街官方网站
  • 南昌定制网站公司百度竞价排名费用
  • 河北省廊坊市三河市最新疫情seo网络推广外包公司
  • 电子书推送网站怎么做线上营销活动主要有哪些
  • 商务网站建设的优势太原seo培训
  • 网站建设挣钱网站网络推广优化
  • 做360手机网站快速搜索引擎简称seo
  • 旅行社网站开发营销策划36计
  • 上国外的网站很慢真正永久免费网站建设
  • 论坛网站建设用工具软件徐州百度seo排名优化
  • 柳州网站seo超级外链工具有用吗
  • 男男互做网站爱站网挖掘词
  • 袁隆平网站设计模板北京最新发布信息
  • seo培训资料网站优化课程
  • 视频网站建设流程图网址生成短链接