题目描述
思路题解
200. 岛屿数量
dfs
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def dfs(grid,i,j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j]=='0':return
grid[i][j]='0'
dfs(grid,i-1,j)
dfs(grid,i,j+1)
dfs(grid,i+1,j)
dfs(grid,i,j-1)
count=0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]=='1':
dfs(grid,i,j)
count+=1
return count
bfs
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def bfs(grid,i,j):
q=[[i,j]]
while q:
[i,j]=q.pop(0)
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j]=='0':continue
grid[i][j]='0'
q+=[[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]
count=0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]=='1':
bfs(grid,i,j)
count+=1
return count
并查集
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def find(x,y):
if father[(x,y)]==(x,y):
return (x,y)
father[(x,y)]=find(father[(x,y)][0],father[(x,y)][1])
return father[(x,y)]
def union(x1,y1,x2,y2):
father[(x2,y2)]=find(x1,y1)
def searchNode(x,y):
for i,j in [[0,1],[0,-1],[1,0],[-1,0]]:
if x+i>=0 and x+i<len(arr) and y+j>=0 and y+j<len(arr[0]) and visited[x+i][y+j]==0 and arr[x+i][y+j]!=0:
visited[x+i][y+j]=1
union(x,y,x+i,y+j)
searchNode(x+i, y+j)
def countisland(arr):
nonlocal ans
for i in range(len(arr)):
for j in range(len(arr[0])):
if arr[i][j]==1:
father[(i,j)]=(i,j)
for i in range(len(arr)):
for j in range(len(arr[0])):
if arr[i][j]==1 and father[(i,j)]==(i,j):
ans+=1
visited[i][j]=1
searchNode(i,j)
else:
visited[i][j]=1
arr=[[0]*len(grid[0]) for _ in range(len(grid))]
for i in range(len(grid)):
for j in range(len(grid[0])):
arr[i][j]=int(grid[i][j])
ans=0
father={}
visited=[[0]*len(arr[0]) for _ in range(len(arr))]
countisland(arr)
return ans
305. 岛屿数量 II
用并查集,每次有加入的岛屿就union,注意计算加入的点与四周的变化的大小关系。
class Solution:
def numIslands2(self, m: int, n: int, positions: List[List[int]]) -> List[int]:
def find(x,y):
if father[(x,y)]==(x,y):
return (x,y)
i,j=father[(x,y)]
# print(i,j,x,y,father[(x,y)])
father[(x,y)]=find(i,j)
return father[(x,y)]
def union(x1,y1,x2,y2):
father[(x2,y2)]=find(x1,y1)
def countIsland(positions,arr):
nonlocal ans
for x,y in positions:
arr[x][y]=1
if visited[x][y]==1:
res.append(ans)
continue
visited[x][y]=1
father[(x,y)]=(x,y)
neighbor={}
for i,j in [[0,1],[0,-1],[1,0],[-1,0]]:
if x+i>=0 and x+i<m and y+j>=0 and y+j<n and arr[x+i][y+j]==1:
#print(x,y,"+++++",x+i,y+j,father[(x+i,y+j)],find(x+i,y+j))
neighbor[find(x+i,y+j)]=1
for i,j in [[0,1],[0,-1],[1,0],[-1,0]]:
if x+i>=0 and x+i<m and y+j>=0 and y+j<n and arr[x+i][y+j]==1:
father[find(x+i,y+j)]=find(x,y)
# print(x,y,neighbor,"----",father)
ans=ans-(len(neighbor)-1)
res.append(ans)
arr=[[0]*n for _ in range(m)]
ans=0
res=[]
father={}
visited=[[0]*len(arr[0]) for _ in range(len(arr))]
countIsland(positions,arr)
return res
108ms的大佬代码:
class Solution:
def numIslands2(self, m: int, n: int, positions: List[List[int]]) -> List[int]:
ids = {}
ans = []
num_islands = 0
for i, j in positions:
num_islands -= self.union(ids, i, j)
ans.append(num_islands)
return ans
def find(self, ids, i, j):
t = (i, j)
while t in ids and ids[t] != t:
ids[t] = ids[ids[t]]
t = ids[t]
return t
def union(self, ids, i, j):
if (i, j) in ids:
return 0
t = (i, j)
ids[t] = t
disappeared = 0
if (i - 1, j) in ids:
ni, nj = self.find(ids, i - 1, j)
if (ni, nj) != t:
disappeared += 1
ids[(ni, nj)] = t
if (i + 1, j) in ids:
ni, nj = self.find(ids, i + 1, j)
if (ni, nj) != t:
disappeared += 1
ids[ni, nj] = t
if (i, j - 1) in ids:
ni, nj = self.find(ids, i, j - 1)
if (ni, nj) != t:
disappeared += 1
ids[(ni, nj)] = t
if (i, j + 1) in ids:
ni, nj = self.find(ids, i, j + 1)
if (ni, nj) != t:
disappeared += 1
ids[ni, nj] = t
return disappeared - 1