Leetcode 208. 实现 Trie (前缀树)(DAY 86) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首学前缀树)


class Trie {
public:
    /** Initialize your data structure here. */
    vector<Trie*> children;
    bool isEnd;
    
    //三刷后 系统学习完c++补充
	//这种不在初始化列表初始化的习惯 相当糟糕
    Trie() {children = vector<Trie*>(26,nullptr); isEnd = false;}
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie* temp = this;
        for(const char& chr:word)
        {
            if(temp->children[chr-'a'] == nullptr)
                temp->children[chr-'a'] = new Trie();
            temp = temp->children[chr-'a'];
        }
        temp->isEnd = true;
    }

    /** Returns if the word is in the trie. */
    bool search(string word) {
        auto temp = this;
        for(const auto& chr:word)
        {
            if(temp->children[chr-'a'] == nullptr)  return false;
            temp = temp->children[chr-'a'];
        }
        if(temp->isEnd) return true;
        else    return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        auto temp = this;
        for(const auto& chr:prefix)
        {
            if(temp->children[chr-'a'] == nullptr)  return false;
            temp = temp->children[chr-'a'];
        }
        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */

代码实现(二刷自解 DAY 144 C++)


class Trie {
public:
    vector<Trie*> trie_tree;
    bool isend;
    Trie():trie_tree(26,nullptr),isend(false) {}
    
    void insert(string word) {
        int size = word.size();
        auto ptr = this;
        for(int i = 0;i < size;++i)
        {
            auto chr = word[i];
            if(!ptr->trie_tree[chr - 'a'])
                ptr->trie_tree[chr - 'a'] = new Trie();
            
            ptr = ptr->trie_tree[chr - 'a'];
        }
        ptr->isend = true;
    }
    
    bool search(string word) const {
        auto ptr = this;
        for(const auto& chr:word)
        {
            if(!ptr->trie_tree[chr - 'a'])
                return false;
            ptr = ptr->trie_tree[chr - 'a'];
        }
        return ptr->isend;
    }
    
    bool startsWith(string prefix) const {
        auto ptr = this;
        for(const auto& chr:prefix)
        {
            if(!ptr->trie_tree[chr - 'a'])
                return false;
            ptr = ptr->trie_tree[chr - 'a'];
        }

        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Love 6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值