Implement Trie (Prefix Tree) - LeetCode
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
思路:应该就是字典树。
class TrieNode {
public:
TrieNode *dic[];
// Initialize your data structure here.
TrieNode() {
for (int i = ; i < ; i++)
dic[i] = NULL;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode* cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dic[loc] == NULL)
cur->dic[loc] = new TrieNode();
cur = cur->dic[loc];
}
if (cur->dic[] == NULL)
cur->dic[] = new TrieNode();
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dic[loc] == NULL) return false;
cur = cur->dic[loc];
}
if (cur->dic[] == NULL) return false;
return true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* cur = root;
for (int i = , len = prefix.size(); i < len; i++)
{
int loc = (int)(prefix[i] - 'a');
if (cur->dic[loc] == NULL) return false;
cur = cur->dic[loc];
}
return true;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
Implement Trie (Prefix Tree) - LeetCode的更多相关文章
- Implement Trie (Prefix Tree) ——LeetCode
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
随机推荐
- 校内考试之zay与银临(day1)
T1大美江湖(洛谷P5006) zayの题解: 这个题的本质是模拟 不过有卡ceil的地方 ceil是对一个double进行向上取整,而对于int/int来说,返回值是int 举个生动的栗子 ceil ...
- 【Python】函数参数类型及用法
一.函数的参数类型 def hs(a1,a2,a3,...): ****statements 其中a1,a2,a3是函数的参数,函数的参数类型可分为:必须参数.默认参数.可变参数(不定长参数).关键 ...
- “帮你APP”团队冲刺8
1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...
- 数据库——MySQL进阶
1.索引 2.触发器 3.存储过程 4.函数 5.事务 6.视图 1.索引 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位 ...
- ffmpeg转换参数和对几种视频格式的转换分析
我们在将多种格式的视频转换成flv格式的时候,我们关注的就是转换后的flv视频的品质和大小.下面就自己的实践所得来和大家分享一下,主要针对avi.3gp.mp4和wmv四种格式来进行分析.通常在使用f ...
- Python框架之Django学习笔记(五)
第一个Django网页小结 进来的请求转入/hello/. Django通过在ROOT_URLCONF配置来决定根URLconf. Django在URLconf中的所有URL模式中,查找第一个匹配/h ...
- leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
- 第一章:前端布局之display属性
css布局学习网站:http://zh.learnlayout.com
- URL 传参中需要处理的特殊字符
例如实际请求URL如下: http://www.douwansha.com/mdeditor?data=[{"address":null,"name":&quo ...
- python re模块详解
re模块 re模块使用python拥有全部的正则表达式功能 1 2 3 4 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法) re.M(MULTILINE):(多行模式,改变 ...