最新做到leetcode_79题,看了一下,发现和我之前一下公司的面试题类似,当时只是写了个解题思路,觉得有点复杂,因此败北;这次认真做了一下,并对比了网友的做法,然后在次基础上对自己算法做改进,最后通过了,耗时也比较少。
# coding=utf-8
# import copy
# 按照大神的指示,用辅助数组visited,成功避免了数组的深拷贝(非常的占内存且耗时),运行速度很快
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
arr = [0]
if not len(word):
return arr[0]
str_index = 0
visited = {}
for i in range(0, len(board)):
for j in range(0, len(board[0])):
if arr[0]:
return arr[0]
if board[i][j] == word[str_index]:
visited[(i, j)] = True
self.recursion(board, word, i, j, str_index+1, arr, visited)
del visited[(i, j)]
return arr[0]
def recursion(self, board, word, index1, index2, str_index, arr, visited):
if str_index == len(word):
arr[0] = 1
return
else:
# 向上
if index1 > 0 and board[index1 - 1][index2] == word[str_index] and arr[0] == 0 and not visited.get((index1 - 1, index2)):
visited[(index1 - 1, index2)] = True
self.recursion(board, word, index1 - 1, index2, str_index + 1, arr, visited)
del visited[(index1 - 1, index2)]
# 向下
if index1 < len(board) - 1 and board[index1 + 1][index2] == word[str_index] and arr[0] == 0 and not visited.get((index1 + 1, index2)):
visited[(index1 + 1, index2)] = True
self.recursion(board, word, index1 + 1, index2, str_index + 1, arr, visited)
del visited[(index1 + 1, index2)]
# 向左
if index2 > 0 and board[index1][index2 - 1] == word[str_index] and arr[0] == 0 and not visited.get((index1, index2 - 1)):
visited[(index1, index2 - 1)] = True
self.recursion(board, word, index1, index2 - 1, str_index + 1, arr, visited)
del visited[(index1, index2 - 1)]
# 向右
if index2 < len(board[0]) - 1 and board[index1][index2 + 1] == word[str_index] and arr[0] == 0 and not visited.get((index1, index2 + 1)):
visited[(index1, index2 + 1)] = True
self.recursion(board, word, index1, index2 + 1, str_index + 1, arr, visited)
del visited[(index1, index2 + 1)]
# # 原来的方法,二维数组的深拷贝,直接导致TLE
# # coding=utf-8
# import copy
#
# def recursion(board, word, index1, index2, str_index, arr):
# if str_index == len(word):
# arr[0] = 1
# return
# else:
# # 向上
# if index1 > 0 and board[index1-1][index2] == word[str_index] and arr[0] == 0:
# ass_board = copy.deepcopy(board)
# ass_board[index1-1][index2] = 0
# recursion(ass_board, word, index1-1, index2, str_index + 1, arr)
# # 向下
# if index1 < len(board)-1 and board[index1+1][index2] == word[str_index] and arr[0] == 0:
# ass_board = copy.deepcopy(board)
# ass_board[index1+1][index2] = 0
# recursion(ass_board, word, index1+1, index2, str_index + 1, arr)
# # 向左
# if index2 > 0 and board[index1][index2-1] == word[str_index] and arr[0] == 0:
# ass_board = copy.deepcopy(board)
# ass_board[index1][index2-1] = 0
# recursion(ass_board, word, index1, index2-1, str_index + 1, arr)
# # 向右
# if index2 < len(board[0])-1 and board[index1][index2+1] == word[str_index] and arr[0] == 0:
# ass_board = copy.deepcopy(board)
# ass_board[index1][index2 + 1] = 0
# recursion(ass_board, word, index1, index2+1, str_index + 1, arr)
#
#
# class Solution(object):
# def exist(self, board, word):
# """
# :type board: List[List[str]]
# :type word: str
# :rtype: bool
# """
# arr = [0]
# if not len(word):
# return arr[0]
# str_index = 0
# for i in range(0, len(board)):
# for j in range(0, len(board[0])):
# if arr[0]:
# return arr[0]
# if board[i][j] == word[str_index]:
# ass_board = copy.deepcopy(board)
# ass_board[i][j] = 0
# recursion(ass_board, word, i, j, str_index+1, arr)
# return arr[0]
leetcode_79题的解析
最新推荐文章于 2023-02-18 18:22:18 发布