solution à un problème LeetCode | 720. mot le plus long dans le dictionnaire (arbre Trie Trie C ++ de)

Titre Description (simple difficulté)

lien Titre original
Insérer ici l'image Description

C code de 1

class Trie {
private:
    //每个Trie节点有一个isEnd变量, 一个包含26个指针的next数组
    Trie *next[26] = {nullptr};
    bool isEnd = false;

public:
    Trie() {
    }

    void insert(const string &word) { // 插入单词
        Trie *root = this;
        for (const auto &ch : word) {
            char w = ch - 'a';
            if (root->next[w] == nullptr) root->next[w] = new Trie();
            root = root->next[w];
        }
        root->isEnd = true;
    }

    bool search(const string &word) { // 查找单词
        Trie *root = this;
        for (const auto &ch : word) {
            char w = ch - 'a';
            if (root->next[w] == nullptr) return false;
            root = root->next[w];
        }
        return root->isEnd;
    }
};


class Solution {
public:
    string Max(string str1, string str2) {
        int n1 = str1.size(), n2 = str2.size();
        if (n1 > n2) return str1;
        else if (n1 < n2) return str2;
        else return min(str1, str2);
    }
    
    string longestWord(vector<string>& words) {
        // 1.把每个单词加入到trie树中
        // 2.遍历每个单词,只有当这个单词的每个字符的isEnd都是true时,才去统计res
        Trie trie;
        for (auto word : words) trie.insert(word);
        
        string res = "";
        for (auto word : words) {
            int i;
            for (i = 0; i < word.size(); i ++) {
                if (trie.search(word.substr(0, i + 1))) continue;
                else break;
            }
            if (i == word.size()) {
                res = Max(res, word);
            }
        }
        
        return res;
    }
};

Insérer ici l'image Description

Le code C de 2

class Trie {
public:
    //每个Trie节点有一个isEnd变量, 一个包含26个指针的next数组
    bool isEnd;
    vector<Trie*> next;
    Trie() : isEnd(false), next(26) {
    }

    void insert(const string &word) { // 插入单词
        Trie *root = this;
        for (const auto &ch : word) {
            char w = ch - 'a';
            if (root->next[w] == nullptr) root->next[w] = new Trie();
            root = root->next[w];
        }
        root->isEnd = true;
    }

    bool search(const string &word) { // 查找单词
        Trie *root = this;
        for (const auto &ch : word) {
            char w = ch - 'a';
            // 直接判断每个字符是否是前缀
            if (root->next[w]->isEnd == false ) return false;
            root = root->next[w];
        }
        return root->isEnd;
    }
};


class Solution {
public:
    string Max(string str1, string str2) {
        int n1 = str1.size(), n2 = str2.size();
        if (n1 > n2) return str1;
        else if (n1 < n2) return str2;
        else return min(str1, str2);
    }
    
    string longestWord(vector<string>& words) {
        // 1.把每个单词加入到trie树中
        // 2.遍历每个单词,而此时检索一定不会出现空指针,修改search函数,去判断isEnd即可
        Trie trie;
        for (auto word : words) trie.insert(word);
        
        string res = "";
        for (auto word : words) {
            if (trie.search(word)) res = Max(res, word);
        }
        
        return res;
    }
};

Insérer ici l'image Description


Écrit dans la dernière : mon blog principalement sur le domaine des connaissances résumées de la science informatique de la pensée et de l' examen, d'écrire chaque blog il est facile de comprendre mon objectif, le partage de la technologie et de la connaissance est un plaisir , et je salue tout le monde ainsi que l'échange de l' apprentissage, il ne peut être question dans la zone de commentaires, mais aussi regarder en avant avec votre échanges en profondeur (^ ∀ ^ ●)

Publié 308 articles originaux · éloge de won 149 · Vues 150 000 +

Je suppose que tu aimes

Origine blog.csdn.net/qq_43827595/article/details/104794741
conseillé
Classement