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

在线A视频网站l一级A做爰片江西中创建设有限公司网站

在线A视频网站l一级A做爰片,江西中创建设有限公司网站,zencart 网站迁移,德州网站建设德州代码随想录算法训练营Day 63| 图论 part03 | 417.太平洋大西洋水流问题、827.最大人工岛、127. 单词接龙 文章目录 代码随想录算法训练营Day 63| 图论 part03 | 417.太平洋大西洋水流问题、827.最大人工岛、127. 单词接龙17.太平洋大西洋水流问题一、DFS二、BFS三、本题总结 82…

代码随想录算法训练营Day 63| 图论 part03 | 417.太平洋大西洋水流问题、827.最大人工岛、127. 单词接龙


文章目录

  • 代码随想录算法训练营Day 63| 图论 part03 | 417.太平洋大西洋水流问题、827.最大人工岛、127. 单词接龙
  • 17.太平洋大西洋水流问题
    • 一、DFS
    • 二、BFS
    • 三、本题总结
  • 827.最大人工岛
    • 一、DFS 用全局变量得到area
    • 二、DFS 用局部变量
    • 三、BFS
  • 127. 单词接龙
    • 一、BFS


17.太平洋大西洋水流问题

题目链接

一、DFS

class Solution(object):def pacificAtlantic(self, heights):""":type heights: List[List[int]]:rtype: List[List[int]]"""m,n=len(heights),len(heights[0])dirs = [(-1,0),(0,1),(1,0),(0,-1)]pacific=[[0]*n for _ in range(m)]atlantic=[[0]*n for _ in range(m)]result=[] # DFSdef dfs(x,y,ocean):ocean[x][y]=1for d in dirs:nextx,nexty=x+d[0],y+d[1]if 0 <= nextx < m and 0 <= nexty < n  and heights[nextx][nexty] >=heights[x][y] and ocean[nextx][nexty]==0:dfs(nextx,nexty,ocean)for i in range(m):dfs(i,0,pacific)dfs(i,n-1,atlantic)for j in range(n):dfs(0,j,pacific)dfs(m-1,j,atlantic)for i in range(m):for j in range(n):if pacific[i][j]==1 and atlantic[i][j]==1:result.append([i,j])return result

二、BFS

class Solution(object):def pacificAtlantic(self, heights):""":type heights: List[List[int]]:rtype: List[List[int]]"""m,n=len(heights),len(heights[0])dirs = [(-1,0),(0,1),(1,0),(0,-1)]pacific=[[0]*n for _ in range(m)]atlantic=[[0]*n for _ in range(m)]result=[] # BFSdef bfs(x,y,ocean):q=collections.deque()q.append((x,y))ocean[x][y]=1while q:x,y = q.popleft()for d in dirs:nextx,nexty=x+d[0],y+d[1]if 0 <= nextx < m and 0 <= nexty < n  and heights[nextx][nexty] >=heights[x][y] and ocean[nextx][nexty]==0:ocean[nextx][nexty]=1q.append((nextx,nexty))for i in range(m):bfs(i,0,pacific)bfs(i,n-1,atlantic)for j in range(n):bfs(0,j,pacific)bfs(m-1,j,atlantic)for i in range(m):for j in range(n):if pacific[i][j]==1 and atlantic[i][j]==1:result.append([i,j])return result

三、本题总结

用两个visited来表示


827.最大人工岛

题目链接

一、DFS 用全局变量得到area

class Solution(object):def largestIsland(self, grid):""":type grid: List[List[int]]:rtype: int"""'''总体思路利用 DFS 计算出各个岛屿的面积,并标记每个 1(陆地格子)属于哪个岛。遍历每个 0,统计其上下左右四个相邻格子所属岛屿的编号,去重后,累加这些岛的面积,更新答案的最大值。'''m,n = len(grid),len(grid[0])dirs = [(-1,0),(0,1),(1,0),(0,-1)]area = collections.defaultdict(int) # 用于储存岛屿面积def dfs(x,y,island_num): # 输入岛屿编号grid[x][y]=island_num area[island_num] += 1 # 更新岛屿面积for d in dirs:nextx,nexty=x+d[0],y+d[1]if 0 <= nextx < m and 0 <= nexty < n  and grid[nextx][nexty]==1:grid[nextx][nexty]=island_numdfs(nextx,nexty,island_num)island_num = 1 for i in range(m):for j in range(n):if grid[i][j]==1: # 遇到新岛屿island_num += 1 # 岛屿编号从2开始dfs(i,j,island_num) ans=0for i in range(m):for j in range(n):s=set() # 去重if grid[i][j]==0:for d in dirs:nexti,nextj=i+d[0],j+d[1]if 0 <= nexti < m and 0 <= nextj < n  and grid[nexti][nextj]!=0:s.add(grid[nexti][nextj])ans = max(ans,1+sum(area[idx] for idx in s))return ans if ans else n*n # 如果最后 ans 仍然为 0,说明所有格子都是 1,返回 n^2

二、DFS 用局部变量

class Solution(object):def largestIsland(self, grid):""":type grid: List[List[int]]:rtype: int"""'''总体思路
利用 DFS 计算出各个岛屿的面积,并标记每个 1(陆地格子)属于哪个岛。
遍历每个 0,统计其上下左右四个相邻格子所属岛屿的编号,去重后,累加这些岛的面积,更新答案的最大值。'''m,n = len(grid),len(grid[0])dirs = [(-1,0),(0,1),(1,0),(0,-1)]area = collections.defaultdict(int) # 用于储存岛屿面积def dfs(x,y,island_num): # 输入岛屿编号grid[x][y]=island_num size=1# area[island_num] += 1 # 更新岛屿面积for d in dirs:nextx,nexty=x+d[0],y+d[1]if 0 <= nextx < m and 0 <= nexty < n  and grid[nextx][nexty]==1:grid[nextx][nexty]=island_numsize += dfs(nextx,nexty,island_num)return size # 得到岛屿的面积island_num = 1 for i in range(m):for j in range(n):if grid[i][j]==1: # 遇到新岛屿island_num += 1 # 岛屿编号从2开始area[island_num]=dfs(i,j,island_num) ans=0for i in range(m):for j in range(n):s=set() # 去重if grid[i][j]==0:for d in dirs:nexti,nextj=i+d[0],j+d[1]if 0 <= nexti < m and 0 <= nextj < n  and grid[nexti][nextj]!=0:s.add(grid[nexti][nextj])ans = max(ans,1+sum(area[idx] for idx in s))return ans if ans else n*n # 如果最后 ans 仍然为 0,说明所有格子都是 1,返回 n^2

三、BFS

class Solution(object):def largestIsland(self, grid):""":type grid: List[List[int]]:rtype: int"""'''总体思路
利用 DFS 计算出各个岛屿的面积,并标记每个 1(陆地格子)属于哪个岛。
遍历每个 0,统计其上下左右四个相邻格子所属岛屿的编号,去重后,累加这些岛的面积,更新答案的最大值。'''# BFSdef bfs(x,y,island_num): # 输入岛屿编号grid[x][y]=island_num size=1# area[island_num] += 1 # 更新岛屿面积q=collections.deque()q.append((x,y))while q:x,y=q.popleft()for d in dirs:nextx,nexty=x+d[0],y+d[1]if 0 <= nextx < m and 0 <= nexty < n  and grid[nextx][nexty]==1:grid[nextx][nexty]=island_numq.append((nextx,nexty))size += 1return sizeisland_num = 1 for i in range(m):for j in range(n):if grid[i][j]==1: # 遇到新岛屿island_num += 1 # 岛屿编号从2开始# dfs(i,j,island_num) # 法1area[island_num]=bfs(i,j,island_num) ans=0for i in range(m):for j in range(n):s=set() # 去重if grid[i][j]==0:for d in dirs:nexti,nextj=i+d[0],j+d[1]if 0 <= nexti < m and 0 <= nextj < n  and grid[nexti][nextj]!=0:s.add(grid[nexti][nextj])ans = max(ans,1+sum(area[idx] for idx in s))return ans if ans else n*n # 如果最后 ans 仍然为 0,说明所有格子都是 1,返回 n^2

127. 单词接龙

题目链接

在这里插入图片描述
在这里插入图片描述

一、BFS

class Solution(object):def ladderLength(self, beginWord, endWord, wordList):""":type beginWord: str:type endWord: str:type wordList: List[str]:rtype: int"""wordset = set(wordList)if len(wordList)==0 or endWord not in wordset :return 0q = collections.deque()q.append(beginWord)visited=set(beginWord)step=1while q:level = len(q)for l in range(level):word = q.popleft()word_list = list(word)for i in range(len(word_list)):origin_char=word_list[i]for j in range(26):word_list[i] = chr(ord('a')+j)new_word = ''.join(word_list)if new_word in wordset:if new_word == endWord:return step+1if new_word not in visited:q.append(new_word)visited.add(new_word)word_list[i]=origin_charstep +=1return 0  
http://www.yayakq.cn/news/769330/

相关文章:

  • 网站怎么做效果好wordpress增加页面分类
  • 融水县住房和城乡建设局网站沈阳网络公司排名
  • 吴忠建设网站flash做ppt的模板下载网站
  • 微信网站开发文档免费托管wordpress
  • 有声阅读网站如何建设ai怎么做自己的网站
  • 哈尔滨企业建站服务商凡科送审平台登录入口
  • 网站建设完整代码北京做网站比较大的公司
  • 网站建设 签约信息家居设计网站模板
  • 网站开发公司会计科目中国建设银行网站能查流水吗
  • 如何创造网站建设公司网站建设报价
  • 福田企业网站优化方案网络框架
  • 用python网站开发深圳网站建设 联雅网络
  • 公司建立网站的意义网上书店网站建设实训总结
  • 做公司网站哪家好 上海佛山国内快速建站
  • 免费网站建设价格费用网站建设h5是指的那一块
  • 表白网站是怎么做的建设网站江西
  • 网站建设哪里有学什么是网络营销发展的助推器
  • 网站建设与管理适合女生吗查询域名后缀网站
  • 平邑网站建设电子商务网站建设需求分析
  • 素材网站软文推广发稿平台
  • 网站排名优化首页广告设计专业周记
  • 企业门户网站管理办法番禺网站建设
  • 威海建设公司网站南宁市建设工程造价信息网
  • 广州品牌设计网站建设网站建设中项目经理的职责
  • 肇庆高端品牌网站建设听说上海又要封了
  • 温州外贸网站推广建设一个网站大概需要多少钱
  • 上海专业网站设计做网站的一般多少钱
  • 网站快排是怎么做的云开发布道师
  • 什么是网站外部链接郑州市建网站
  • 一个网站主机多少钱一年淘宝联盟 wordpress