代码随想录算法训练营Day 63| 图论 part03 | 417.太平洋大西洋水流问题、827.最大人工岛、127. 单词接龙
创始人
2024-11-24 23:08:01
0

代码随想录算法训练营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=[]          # DFS         def dfs(x,y,ocean):             ocean[x][y]=1             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:                     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=[]          # BFS         def bfs(x,y,ocean):             q=collections.deque()             q.append((x,y))             ocean[x][y]=1             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 heights[nextx][nexty] >=heights[x][y] and ocean[nextx][nexty]==0:                         ocean[nextx][nexty]=1                         q.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_num                     dfs(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=0         for 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_num                     size += 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=0         for 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,统计其上下左右四个相邻格子所属岛屿的编号,去重后,累加这些岛的面积,更新答案的最大值。         '''         # BFS         def 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_num                         q.append((nextx,nexty))                         size += 1             return size                      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) # 法1                     area[island_num]=bfs(i,j,island_num)          ans=0         for 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 0         q = collections.deque()         q.append(beginWord)         visited=set(beginWord)         step=1         while 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+1                             if new_word not in visited:                                 q.append(new_word)                                 visited.add(new_word)                     word_list[i]=origin_char             step +=1         return 0   

相关内容

热门资讯

分享!炸金花的房卡哪里买/微信... 微信游戏中心:炸金花房卡,添加微信【33699510】,进入游戏中心或相关小程序,搜索“微信炸金花房...
微信牛牛房卡招代理/炸金花房卡... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
一分钟了解“微信斗牛牛房卡使用... 微信斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
金花大厅房卡如何购买的/微信开... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
链接金花房卡哪里买/微信群牛牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
金花房卡一手货源/微信链接炸金... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
金花房卡链接怎么购买/金花斗牛... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
给大家讲解“微信拼三张房卡怎么... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
秒懂教程“微信牛牛群哪里购买房... 人海大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
牛牛在哪里购买房卡/哪里购买斗... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
正版授权“牛牛链接房卡那里有/... 海贝之城是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
在哪里能买金花房卡/微信里面炸... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
微信金花房卡怎样购买/牛牛链接... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
一分钟了解“软件炸金花模式创建... 新漫游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房...
微信牛牛链接怎么制作/微信斗牛... 微信斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
房卡必备教程“微信斗牛牛小程序... 随意玩俱乐部是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房...
秒懂教程“牛牛房卡的客服联系方... 道游大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
金花链接房卡如何充值/微信炸金... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
微信牛牛房卡客服微信/微信开金... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
微信怎么玩金花自建房间步骤/上... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...