首页 > 代码库 > LeetCode 208. Implement Trie (Prefix Tree)
LeetCode 208. Implement Trie (Prefix Tree)
字典树。
測试中有:aaaaaaaaaaa... 的输入,假设每一个节点用定长数组存储孩子的话。那就是26^len的空间复杂度(len为输入的长度),内存会不够的。
所以用map<char, TrieNode*>保存其孩子。
第三遍(将第二遍中search和startsWith的行为抽象成searchNode方法):
struct TrieNode { map<char, shared_ptr<TrieNode>> children; bool is_word = false; }; class Trie { public: Trie(): root(shared_ptr<TrieNode>(new TrieNode)) {} // Inserts a word into the trie. void insert(string word) const { if (word.empty()) { return ; } auto node = root; for (size_t i = 0; i < word.size(); node = node->children[word[i ++]]) { if (node->children.find((word[i])) == node->children.end()) { node->children[word[i]] = shared_ptr<TrieNode>(new TrieNode); } } node->is_word = true; } // Returns if the word is in the trie. bool search(string word) const { auto node = searchNode(word); return node!=nullptr && node->is_word; } // Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string prefix) const { return searchNode(prefix) != nullptr; } private: shared_ptr<TrieNode> searchNode(string word) const { auto node = root; for (size_t i = 0; i < word.size(); node = node->children[word[i ++]]) { if (node->children.find(word[i]) == node->children.end()) { return nullptr; } } return node; } shared_ptr<TrieNode> root; };第二遍(将第一次代码中的尾递归改成了迭代,速度从270ms提升到了200ms):
struct TrieNode { map<char, shared_ptr<TrieNode>> children; bool is_word = false; }; class Trie { public: Trie(): root(shared_ptr<TrieNode>(new TrieNode)) {} // Inserts a word into the trie. void insert(string word) const { if (word.empty()) { return ; } auto node = root; for (size_t i = 0; i < word.size(); node = node->children[word[i ++]]) { if (node->children.find((word[i])) == node->children.end()) { node->children[word[i]] = shared_ptr<TrieNode>(new TrieNode); } } node->is_word = true; } // Returns if the word is in the trie. bool search(string word) const { // no special judge for empty word here auto node = root; for (size_t i = 0; i < word.size(); node = node->children[word[i ++]]) { if (node->children.find(word[i]) == node->children.end()) { return false; } } return node->is_word; } // Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string prefix) const { auto node = root; for (size_t i = 0; i < prefix.size(); node = node->children[prefix[i ++]]) { if (node->children.find(prefix[i]) == node->children.end()) { return false; } } return true; } private: shared_ptr<TrieNode> root; };
第一遍:
class TrieNode { public: // Initialize your data structure here. TrieNode() { is_word = false; } void insert(const string& word) { if (!word.empty()) { nodes[word.front()] = nodes[word.front()] != nullptr ? nodes[word.front()] : new TrieNode(); nodes[word.front()]->insert(word.substr(1)); } else { is_word = true; } } bool search(string word) { if (!word.empty()) { if (nodes[word.front()] == nullptr) { return false; } else { return nodes[word.front()]->search(word.substr(1)); } } return is_word; } bool startsWith(string prefix) { if (!prefix.empty()) { if (nodes[prefix.front()] == nullptr) { return false; } else { return nodes[prefix.front()]->startsWith(prefix.substr(1)); } } return true; } // MLE!!! // TrieNode* nodes[26]; map<char, TrieNode*> nodes; bool is_word; }; class Trie { public: Trie() { root = new TrieNode(); } // Inserts a word into the trie. void insert(string word) { root->insert(word); } // Returns if the word is in the trie. bool search(string word) { return root->search(word); } // Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string prefix) { return root->startsWith(prefix); } private: TrieNode* root; };
LeetCode 208. Implement Trie (Prefix Tree)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。