leetcode__单词搜索II__python

方法1:
将所有单词构建一个前缀树,然后用深度优先搜索来找到board中含有哪些单词。
具体代码如下:

class node:
    def __init__(self):
        self.val = False
        self.next = {
     }


class Trie:
    def __init__(self):
        self.root = node()
        
    def insert(self, word):
        tmp_node = self.root
        for char in word:
            if char not in tmp_node.next:
                tmp_node.next[char] = node()
            tmp_node = tmp_node.next[char]
        tmp_node.val = word
        return


class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        if not board or not board[0] or not words:
            return []   
        Trie_tree = Trie()
        for word in words:
            Trie_tree.insert(word)
        res = []
        for i in range(len(board)):
            for j in range(len(board[0])):
                tmp_state = []
                self.dfs(i, j, board, Trie_tree.root, tmp_state, res)
        return res
        
    def dfs(self, i, j, board, node, tmp_state, res):
        if node.val and node.val not in res:
            res.append(node.val) 
        if [i, j] in tmp_state or board[i][j] not in node.next:
            return []
        else:   #[i, j] not in tmp_state        board[i][j] in node.next
            tmp = tmp_state + [[i, j]]
            candidate = []
            if j - 1 >= 0:
                candidate.append([i, j-1])
            if j + 1 < len(board[0]):
                candidate.append([i, j+1])
            if i - 1 >= 0:
                candidate.append([i-1, j])
            if i + 1 < len(board):
                candidate.append([i+1, j])
            node = node.next[board[i][j]]
            if node.val and node.val not in res:
                res.append(node.val)
            for item in candidate:
                self.dfs(item[0], item[1], board, node, tmp, res)

方法2:
直接用默认的dict来解决前缀树的定义问题。
具体代码如下:

class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        if not board or not board[0] or not words:
            return []
        self.root = {
     }
        for word in words:
            node = self.root
            for char in word:
                if char not in node:
                    node[char] = {
     }
                node = node[char]
            node['#'] = word
        res = []
        for i in range(len(board)):
            for j in range(len(board[0])):
                tmp_state = []
                self.dfs(i, j, board, tmp_state, self.root, res)
        return res
        
    def dfs(self, i, j, board, tmp_state, node, res):
        if '#' in node and node['#'] not in res:
            res.append(node['#'])
        if [i, j] not in tmp_state and board[i][j] in node:
            tmp = tmp_state + [[i, j]]
            candidate = []
            if i-1 >= 0:
                candidate.append([i-1, j])
            if i+1 < len(board):
                candidate.append([i+1, j])
            if j-1 >= 0:
                candidate.append([i, j-1])
            if j+1 < len(board[0]):
                candidate.append([i, j+1])
            node = node[board[i][j]]
            if '#' in node and node['#'] not in res:
                res.append(node['#'])
            for item in candidate:
                self.dfs(item[0], item[1], board, tmp, node, res)
        

你可能感兴趣的:(leetcode,python)