题目链接在这里:https://leetcode.com/problems/word-search/
求好心人帮忙看一下为什么代码过不了test case可以吗?好像有的test case是过得了的,这个就不行。
和别人写的对了好久也没看出来。。求指教求帮助!谢谢!
代码:
[Python] 纯文本查看 复制代码 class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def find_next(row, col, index):
# base
if index == len(word):
return True
if row < 0 or row == n_row or col < 0 or col == n_col:
return False
if visited[row][col]:
return False
if board[row][col] != word[index]:
return False
# recursion
else:
visited[row][col] = True
res = find_next(row + 1, col, index + 1) or find_next(row - 1, col, index + 1) or find_next(row, col + 1, index + 1) or find_next(row, col - 1, index + 1)
visited[row][col] = False
return res
# main program
n_row = len(board)
n_col = len(board[0])
visited = [[False] * n_col] * n_row
for row in range(n_row):
for col in range(n_col):
if find_next(row, col, 0):
return True
return False
补充内容 (2020-12-1 03:13):
已解决,谢谢! |