力扣每日一题:208. 实现 Trie (前缀树)

题目:208. 实现 Trie (前缀树)

难度: 中等

题目
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:

  • Trie() 初始化前缀树对象。
  • void insert(String word) 向前缀树中插入字符串 word 。
  • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
  • boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

示例

输入
[“Trie”, “insert”, “search”, “search”, “startsWith”, “insert”, “search”]
[[], [“apple”], [“apple”], [“app”], [“app”], [“app”], [“app”]]
输出
[null, null, true, false, true, null, true]
解释
Trie trie = new Trie();
trie.insert(“apple”);
trie.search(“apple”); // 返回 True
trie.search(“app”); // 返回 False
trie.startsWith(“app”); // 返回 True
trie.insert(“app”);
trie.search(“app”); // 返回 True

提示:

  • 1 <= word.length, prefix.length <= 2000
  • word 和 prefix 仅由小写英文字母组成
  • insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次

来源:力扣(LeetCode)
链接https://leetcode-cn.com/problems/implement-trie-prefix-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。



解题思路

  Trie树,又称前缀树字典树单词查找树。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
  该题就是实现字典树的操作。从根节点出发,每个节点定义26个分支,初始时为NULL。每个节点存储一个英文字母,一个单词从根节点出发往后存储。根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。同时,每个结点都需要一个bool类型参数用来判断当前位置是否是存储一个单词。

解题代码

typedef struct Tree
{
    struct Tree* data[26];
    bool isWord;
}Tree;
class Trie {
private:
    Tree *root;
public:
    /** Initialize your data structure here. */
    Trie() {
        root = (Tree*)calloc(1, sizeof(Tree));
        
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        int n = word.size();
        Tree *t = root;
        for(int i = 0; i < n; i++)
        {
            if(t->data[word[i] - 'a'] == NULL)
                t->data[word[i] - 'a'] = (Tree*)calloc(1, sizeof(Tree));
            t = t->data[word[i] - 'a'];
        }
        t->isWord = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Tree *t = root;
        for(int i = 0; i < word.size(); i++)
        {
            if(t->data[word[i] - 'a'] == NULL)
                return false;
            t = t->data[word[i] - 'a'];
        }
        if(t->isWord)
            return true;
        else
            return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Tree *t = root;
        for(int i = 0; i < prefix.size(); i++)
        {
            if(t->data[prefix[i] - 'a'] == NULL)
                return false;
            t = t->data[prefix[i] - 'a'];
        }
        return true;
    }
};

解题感悟

  学习了字典树的操作。解决该题使用了数组存储数据,实际还可用哈希表等降低空间复杂度。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗夜无风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值