leetcode_79题的解析

最新做到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]    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值