[LeetCode] Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

实现字典树,没啥好说的。

 class TrieNode {
public:
// Initialize your data structure here.
TrieNode *ch[];
bool isKey;
TrieNode() : isKey(false) {
for (auto &a : ch) a = NULL;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string s) {
TrieNode *p = root;
for (auto &a : s) {
int i = a - 'a';
if (p->ch[i] == NULL) p->ch[i] = new TrieNode();
p = p->ch[i];
}
p->isKey = true;
} // Returns if the word is in the trie.
bool search(string key) {
TrieNode *p = root;
for (auto &a : key) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return p->isKey;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *p = root;
for (auto &a : prefix) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
上一篇:css学习(2)-- 常见的CSS属性和值


下一篇:(简单) POJ 3087 Shuffle'm Up,枚举。