2 Star 1 Fork 0

royce li/Leetcode_royce

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wordExist.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
Royce Li 提交于 2021-05-10 16:39 . 2020/5/10 First Commit
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/royce-li/leetcode_royce.git
git@gitee.com:royce-li/leetcode_royce.git
royce-li
leetcode_royce
Leetcode_royce
master

搜索帮助