LeetCode - Implement Trie (Prefix Tree)
2015-08-01 09:42
423 查看
Implement a trie with
and
Note:
You may assume that all inputs are consist of lowercase letters
一开始用了unorered_map,有点慢,换了数组后快了点
class TrieNode {
public:
char val;
TrieNode *children[26];
bool tail;
// Initialize your data structure here.
TrieNode() {
val = '\0';
for(int i = 0; i < 26; ++i)
children[i] = nullptr;
tail = false;
}
TrieNode(char c): val(c){
for(int i = 0; i < 26; ++i)
children[i] = nullptr;
tail = false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode *p = root;
int l = word.length();
for(int i = 0; i < l; ++i){
int j = word[i] - 'a';
if(!p->children[j]){
p->children[j] = new TrieNode(word[i]);
}
p = p->children[j];
}
p->tail = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode *p = root;
int l = word.length();
for(int i = 0; i < l; ++i){
int j = word[i] - 'a';
if(!p->children[j])
return false;
p = p->children[j];
}
return p->tail;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *p = root;
int l = prefix.length();
for(int i = 0; i < l; ++i){
int j = prefix[i] - 'a';
if(!p->children[j])
return false;
p = p->children[j];
}
return true;
}
private:
TrieNode* root;
};
insert,
search,
and
startsWithmethods.
Note:
You may assume that all inputs are consist of lowercase letters
a-z.
一开始用了unorered_map,有点慢,换了数组后快了点
class TrieNode {
public:
char val;
TrieNode *children[26];
bool tail;
// Initialize your data structure here.
TrieNode() {
val = '\0';
for(int i = 0; i < 26; ++i)
children[i] = nullptr;
tail = false;
}
TrieNode(char c): val(c){
for(int i = 0; i < 26; ++i)
children[i] = nullptr;
tail = false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode *p = root;
int l = word.length();
for(int i = 0; i < l; ++i){
int j = word[i] - 'a';
if(!p->children[j]){
p->children[j] = new TrieNode(word[i]);
}
p = p->children[j];
}
p->tail = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode *p = root;
int l = word.length();
for(int i = 0; i < l; ++i){
int j = word[i] - 'a';
if(!p->children[j])
return false;
p = p->children[j];
}
return p->tail;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *p = root;
int l = prefix.length();
for(int i = 0; i < l; ++i){
int j = prefix[i] - 'a';
if(!p->children[j])
return false;
p = p->children[j];
}
return true;
}
private:
TrieNode* root;
};
相关文章推荐
- C#实现获取系统目录并以Tree树叉显示的方法
- C语言实现输入一颗二元查找树并将该树转换为它的镜像
- 纯jsp打造无限层次的树代码
- Python Trie树实现字典排序
- php遍历树的常用方法汇总
- PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
- PHP生成树的方法
- Java中实现双数组Trie树实例
- Java Swing中的表格(JTable)和树(JTree)组件使用实例
- python数据结构树和二叉树简介
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- B+树到MySQL之innoDB
- ExtJS 4 树
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart