leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 73 】2022-02-22 - 实现 Trie (前缀树 #83

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

实现 Trie (前缀树

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/implement-trie-prefix-tree

前置知识

示例:

Trie trie = new Trie();

trie.insert("apple"); trie.search("apple"); // 返回 true trie.search("app"); // 返回 false trie.startsWith("app"); // 返回 true1 trie.insert("app"); trie.search("app"); // 返回 true 说明:

你可以假设所有的输入都是由小写字母 a-z 构成的。 保证所有输入均为非空字符串。

for123s commented 2 years ago
struct triNode
{
    vector<triNode*> child; 
    int preCount; 
    int count; 
    triNode()
    {
        child.assign(26, NULL);
        preCount=0; 
        count =0; 
    }
};

class Trie {
public:
    triNode* root;

    Trie() {
        root = new triNode();    
    }

    void insert(string word) {
        triNode* temp = root;
        for(int i=0;i<word.size();i++)
        {
            int idx = word[i]-'a';
            if(!temp->child[idx])
                temp->child[idx] = new triNode();
            temp->preCount++;
            temp = temp->child[idx];
        }
        temp->count++;
    }

    bool search(string word) {
        triNode* temp = root;
        for(int i=0;i<word.size();i++)
        {
            int idx = word[i]-'a';
            if(!temp->child[idx])
                return false;
            temp = temp->child[idx];
        }
        if(temp->count>0)
            return true;
        return false;
    }

    bool startsWith(string prefix) {
        triNode* temp = root;
        for(int i=0;i<prefix.size();i++)
        {
            int idx = prefix[i]-'a';
            if(!temp->child[idx])
                return false;
            temp = temp->child[idx];
        }
        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);
 */
hulichao commented 2 years ago

思路

这样就可以

代码


class Trie {
    private Set<String> data = new HashSet<>();

    /** Initialize your data structure here. */
    public Trie() {

    }

    /** Inserts a word into the trie. */
    public void insert(String word) {
        data.add(word);
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        return data.contains(word);
    }

    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        if (data.contains(prefix)) {
            return true;
        }

        for (String each: data) {
            if (each.startsWith(prefix)) {
                return true;
            }

        }

        return false;
    }
}

复杂度分析

tian-pengfei commented 2 years ago
class Trie {

private:
    vector<Trie*> children;
    //是不是单词结尾,并不是数的叶子节点的意思
    bool isEnd ;

    Trie* searchPrefix(string prefix){
        Trie * node = this;
        for( auto c: prefix){
            c-='a';
            if(node->children[c] == nullptr){
                return nullptr;
            }
            node = node->children[c];
        }
        return node;
    }
public:
    Trie() :children(26),isEnd(false){

    }

    void insert(string word) {
        Trie *node =this ;
        for (auto c:word){
            c-='a';
            if(node->children[c]== nullptr){
                node->children[c] = new Trie();
            }
            node = node->children[c];
        }
        node->isEnd = true;
    }

    bool search(string word) {
        Trie* node = searchPrefix(word);
        if(node== nullptr||!node->isEnd)return false;
        return true;
    }

    bool startsWith(string prefix) {
        Trie* node = searchPrefix(prefix);
        if(node== nullptr)return false;
        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);
 */