leetcode-字典树

字典树

之前刷了不少leetcode的题目,现在把之前写的代码上传了github:https://github.com/czy36mengfei/leetcode,欢迎大家下载、star。

知识拓展: 字典树

Trie 又称作字典树、前缀树。

Trie 的结构:

1、Trie 的设计很巧妙,它不像一般的字典那样,把一整个单词存在数据结构里。Trie 利用单词的前缀去匹配一棵树,能匹配上,则说明这个字典里有这个单词;

2、Trie 的结构有那么一点点像链表,只不过链表的 next 指针指向的是一个 Node 结点,而 Trienext 指针指向的是一个 Map

3、Trie 本身携带的内容仅仅只是 isWord 这样一个布尔型变量,而沿着 next 指针一路走下来的路径,就能表示一个单词。

下面是一个 Trie 树的基本结构:

class Trie(object):
    class Node:
        def __init__(self):
            self.is_word = False  # 是一个单词结尾的标志
            self.dict = dict()

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = Trie.Node()

    def insert(self, word):
        """
        Inserts a word into the trie.
        :type word: str
        :rtype: None
        """
        node = self.root
        for c in word:
            if c not in node.dict:
                node.dict[c] = Trie.Node()
            node = node.dict[c]
        node.is_word = True


    def search(self, word):
        """
        Returns if the word is in the trie.
        :type word: str
        :rtype: bool
        """
        node = self.root
        for c in word:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return node.is_word

    def startsWith(self, prefix):
        """
        Returns if there is any word in the trie that starts with the given prefix.
        :type prefix: str
        :rtype: bool
        """
        node = self.root
        for c in prefix:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return True

Trie的添加操作:

   def insert(self, word):
        """
        Inserts a word into the trie.
        :type word: str
        :rtype: None
        """
        node = self.root
        for c in word:
            if c not in node.dict:
                node.dict[c] = Trie.Node()
            node = node.dict[c]
        node.is_word = True

Trie的查询操作:

1、理解 Trie 的查询只与待查询的字符串的长度有关

2、下面这个方法查询整个单词在 Trie 中是否存在,所以在路径匹配完成以后,一定不要忘了判断匹配到的那个结点的 isWord 属性,如果它是一个单词的结尾,才返回 True

    def search(self, word):
        """
        Returns if the word is in the trie.
        :type word: str
        :rtype: bool
        """
        node = self.root
        for c in word:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return node.is_word

Trie 的前缀查询操作

前缀查询就更简单了,此时不需要判断 isWord 属性的值,只需要判断从树的根结点是不是很顺利地都能匹配单词的每一个字符。

  def startsWith(self, prefix):
        """
        Returns if there is any word in the trie that starts with the given prefix.
        :type prefix: str
        :rtype: bool
        """
        node = self.root
        for c in prefix:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return True

每日音乐:http://music.163.com/#/m/song?id=22840606

每日一题

添加与搜索单词 - 数据结构设计

设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(".ad") -> true
search(“b…”) -> true
说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值