代码拉取完成,页面将自动刷新
import copy
class Solution:
def exist(self, board, word) -> bool:
width=len(board[0])
height=len(board)
def findletter(subboard,x,y,word):
if len(word)==0:
return True
if x-1>=0:
if subboard[x-1][y]==word[0]:
backup=subboard[x-1][y]
subboard[x-1][y]=0
if findletter(subboard,x-1,y,word[1:]):
return True
else:
subboard[x - 1][y]=backup
if x+1<height :
if subboard[x+1][y]==word[0]:
backup=subboard[x+1][y]
subboard[x+1][y]=0
if findletter(subboard,x+1,y,word[1:]):
return True
else:
subboard[x+1][y]=backup
if y-1>=0:
if subboard[x][y-1]==word[0]:
backup = subboard[x][y-1]
subboard[x][y-1]=0
if findletter(subboard,x,y-1,word[1:]):
return True
else:
subboard[x][y-1]=backup
if y+1<width:
if subboard[x][y+1]==word[0]:
backup=subboard[x][y + 1]
subboard[x][y+1]=0
if findletter(subboard,x,y+1,word[1:]):
return True
else:
subboard[x][y+1]=backup
return False
for i in range(height):
for j in range(width):
if board[i][j]==word[0]:
subboard=copy.deepcopy(board)
subboard[i][j]=0
if findletter(subboard,i,j,word[1:]):
return True
return False
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。