目录
LeetCode题目:面试题 08.10. 颜色填充
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
def dfs(image,sr,sc,oldColor,newColor):
if image[sr][sc] == newColor:
return
image[sr][sc] = newColor
d = [[-1,0],[1,0],[0,-1],[0,1]]
for i in d:
x,y=sr+i[0],sc+i[1]
if x>=0 and x<len(image) and y>=0 and y<len(image[0]) and image[x][y] == oldColor:
dfs(image,x,y,image[x][y],newColor)
# dfs:1)填充当前位置的颜色;
# dfs:2)从当前位置的四周去判断是否需要填充;
dfs(image,sr,sc,image[sr][sc],newColor)
return image
695. 岛屿的最大面积
class Solution(object):
def dfs(self,r,c,raw,col,grid):
if r<0 or c<0 or r>=raw or c>=col or grid[r][c] != 1:
return 0
count = 1
grid[r][c] = 0
count += self.dfs(r-1,c,raw,col,grid)
count += self.dfs(r+1,c,raw,col,grid)
count += self.dfs(r,c+1,raw,col,grid)
count += self.dfs(r,c-1,raw,col,grid)
return count
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
raw = len(grid)
col = len(grid[0])
ret = 0
for r in range(raw):
for c in range(col):
if grid[r][c] == 1:
ret = max(ret,self.dfs(r,c,raw,col,grid))
return ret
547. 省份数量
class Solution(object):
def findCircleNum(self, isConnected):
"""
:type isConnected: List[List[int]]
:rtype: int
"""
raw = len(isConnected)
def dfs(i):
for r in range(raw):
if isConnected[i][r] == 1 and r not in visited:
visited.add(r)
dfs(r)
ret = 0
visited = set()
for r in range(raw):
if r in visited:
continue
dfs(r)
ret+=1
return ret
200. 岛屿数量
class Solution(object):
def dfs(self,r,c,raw,col,grid):
if r<0 or c<0 or r>=raw or c>=col or grid[r][c] == '0':
return
grid[r][c] = '0'
self.dfs(r+1,c,raw,col,grid)
self.dfs(r-1,c,raw,col,grid)
self.dfs(r,c+1,raw,col,grid)
self.dfs(r,c-1,raw,col,grid)
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
raw = len(grid)
col = len(grid[0])
ret = 0
for r in range(raw):
for c in range(col):
if grid[r][c] == '1':
self.dfs(r,c,raw,col,grid)
ret +=1
return ret
130. 被围绕的区域
class Solution(object):
def dfs(self,r,c,raw,col,board):
if r<0 or c<0 or r>=raw or c>=col or board[r][c] != 'O':
return
board[r][c] = '-1'
self.dfs(r-1,c,raw,col,board)
self.dfs(r+1,c,raw,col,board)
self.dfs(r,c+1,raw,col,board)
self.dfs(r,c-1,raw,col,board)
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
raw = len(board)
col = len(board[0])
for c in range(col):
self.dfs(0,c,raw,col,board)
self.dfs(raw-1,c,raw,col,board)
for r in range(raw):
self.dfs(r,0,raw,col,board)
self.dfs(r,col-1,raw,col,board)
for r in range(raw):
for c in range(col):
if board[r][c] == '-1':
board[r][c] = 'O'
else:
board[r][c] = 'X'
79. 单词搜索
class Solution:
def dfs(self,r,c,raw,col,board,word,l,visited):
# 结束条件:word遍历到最后一个,并且最后一个字符匹配上
if l == len(word)-1:
return board[r][c] == word[l]
if board[r][c] == word[l]:
visited.add((r,c))
for i,j in [[-1,0],[1,0],[0,-1],[0,1]]:
c_r,c_c = i+r,j+c
if c_r<0 or c_r>=raw or c_c<0 or c_c>=col or (c_r,c_c) in visited:
continue
if self.dfs(c_r,c_c,raw,col,board,word,l+1,visited):
return True
visited.remove((r,c))
return False
def exist(self, board: List[List[str]], word: str) -> bool:
raw = len(board)
col = len(board[0])
for r in range(raw):
for c in range(col):
visited = set()
if self.dfs(r,c,raw,col,board,word,0,visited):
return True
return False
1020. 飞地的数量
class Solution:
def dfs(self,r,c,raw,col,grid):
if r<0 or r>=raw or c<0 or c>=col or grid[r][c] != 1:
return
grid[r][c] = 0
self.dfs(r+1,c,raw,col,grid)
self.dfs(r-1,c,raw,col,grid)
self.dfs(r,c+1,raw,col,grid)
self.dfs(r,c-1,raw,col,grid)
def numEnclaves(self, grid: List[List[int]]) -> int:
raw = len(grid)
col = len(grid[0])
for r in range(raw):
for c in range(col):
if (r == 0 or r == raw-1) and grid[r][c] == 1:
self.dfs(r,c,raw,col,grid)
if (c == 0 or c == col-1) and grid[r][c] == 1:
self.dfs(r,c,raw,col,grid)
ret = 0
for r in range(raw):
for c in range(col):
if grid[r][c] == 1:
ret+=1
return ret
1254. 统计封闭岛屿的数目
class Solution:
def dfs(self,r,c,raw,col,grid):
if r<0 or r>=raw or c<0 or c>=col or grid[r][c] != 0:
return
grid[r][c] = 1
self.dfs(r+1,c,raw,col,grid)
self.dfs(r-1,c,raw,col,grid)
self.dfs(r,c+1,raw,col,grid)
self.dfs(r,c-1,raw,col,grid)
def closedIsland(self, grid: List[List[int]]) -> int:
raw = len(grid)
col = len(grid[0])
for r in range(raw):
for c in range(col):
if (r == 0 or r == raw-1) and grid[r][c] == 0:
self.dfs(r,c,raw,col,grid)
if (c == 0 or c == col-1) and grid[r][c] == 0:
self.dfs(r,c,raw,col,grid)
ret = 0
for r in range(raw):
for c in range(col):
if grid[r][c] == 0:
self.dfs(r,c,raw,col,grid)
ret+=1
return ret
剑指 Offer 13. 机器人的运动范围
class Solution:
def dfs(self,r,c,m,n,k,visited):
if r>=m or c>=n or (r,c) in visited or int(r%10+r//10+c%10+c//10) > k:
return 0
visited.add((r,c))
count = 1
count+=self.dfs(r+1,c,m,n,k,visited)
count+=self.dfs(r,c+1,m,n,k,visited)
return count
def movingCount(self, m: int, n: int, k: int) -> int:
visited = set()
return self.dfs(0,0,m,n,k,visited)
323. 无向图中连通分量的数目
class Solution:
def dfs(self,i,visited,dict_):
if i in visited:
return
visited.add(i)
if i in dict_:
for j in dict_[i]:
self.dfs(j,visited,dict_)
def countComponents(self, n: int, edges: List[List[int]]) -> int:
dict_ = {}
for i,j in edges:
dict_[i]=dict_.get(i,[])+[j]
dict_[j]=dict_.get(j,[])+[i]
visited=set()
ret = 0
for i in range(n):
if i not in visited:
ret+=1
if i in dict_:
self.dfs(i,visited,dict_)
else:
visited.add(i)
return ret