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 ...
随机推荐
- 14,UA池和代理池
今日概要 scrapy下载中间件 UA池 代理池 一,下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用: (1)引擎将请求传递给下载器 ...
- 启动子Activity
启动普通子Activity: 一个activity启动另一个activity最简单的方式是使用 startActivity(Intent) 方法: public void startActivity( ...
- 网络安全巧设置 Win2008 R2 防火墙详解(1)
针对一般中小企业型来说,如果希望对企业网络进行安全管理,不一定非得花高价钱购买专业的防火墙设置,直接借助操作系统本身自带的防火墙功能即可以满足一般企业的应用,今天我们就一起来探究一下Windows S ...
- StartWith 测试
var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...
- leetcode 【 Partition List 】python 实现
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- 什么是事务?MySQL如何支持事务?
什么是事务? 事务是由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行.程序和事务是两个不同的概念.一般而言:一段程序中可能包含多个事务.(说白了就是几步的数据库操作 ...
- python拼接
拼接: name=zhuhuan age=23 salary=333 info=''' ----- info of %s----- age:%s name:%s salary:%s %(name,ag ...
- jquery怎样获取html页面中的data-xxx
$(this).attr("data-id") // will return the string "123"or .data() (if you use ne ...
- LAMP第三部分php配置和mysql配置
9. 配置防盗链http://www.lishiming.net/thread-71-1-1.html 防止别人的网站,放你网站图片的链接, 位置一般情况下在 /usr/local/apache/co ...
- linux 相关知识
在mac 终端中可以直接访问ssh 命令:ssh root@127.0.0.* 批量删除文件: 当前目录下所有 *.html文件,除了index.html [root@whr ...