leetcode__单词搜索II__python

博客介绍了两种解决单词搜索问题的方法。方法一是将所有单词构建前缀树,用深度优先搜索查找board中含有的单词;方法二则是用默认的dict解决前缀树的定义问题,并给出了具体代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法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)
        
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值