变量简洁正确完整思路
Tires,isend和Tires*next[26]
class Trie {
public:
    Trie() {
        isEnd=false;
        memset(next,0,sizeof(next));
    }
    void insert(string word) {
        Trie*node=this;
        for(char c:word){
            if(!node->next[c-'a'])node->next[c-'a']=new Trie();
            node=node->next[c-'a'];
        }
        node->isEnd=true;
    }
    bool search(string word) {
        Trie*node=this;
        for(char c:word){
            node=node->next[c-'a'];
            if(!node)return false;
        }
        return node->isEnd;
    }
    bool startsWith(string prefix) {
        Trie*node=this;
        for(char c:prefix){
            node=node->next[c-'a'];
            if(!node)return false;
        }
        return true;
    }
private:
    bool isEnd;
    Trie*next[26];
};
踩过的坑
this指针不能赋值不能改变,所以Tries*node=this;

 

posted on 2021-08-07 12:43  offer快到碗里来~  阅读(51)  评论(0)    收藏  举报