【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
一个字母代表一个子树,因此为26叉树,end标记表示是否存在以该字母为结尾的字符串。
class TrieNode {
public:
TrieNode* children[];
bool end;
// Initialize your data structure here.
TrieNode() {
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size())
{
if(curNode->end == true)
// exist
return;
else
// insert
curNode->end = true;
}
else
{
while(i < word.size())
{
curNode->children[word[i]-'a'] = new TrieNode();
curNode = curNode->children[word[i]-'a'];
i ++;
}
curNode->end = true;
}
}
// Returns if the word is in the trie.
bool search(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size() && curNode->end == true)
// curNode must be leaf
return true;
else
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i = ;
TrieNode* curNode = root;
while(i < prefix.size() && curNode->children[prefix[i]-'a'] != NULL)
{
curNode = curNode->children[prefix[i]-'a'];
i ++;
}
if(i == prefix.size())
// curNode might be left or not
return true;
else
return false;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

【LeetCode】208. Implement Trie (Prefix Tree)的更多相关文章
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 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) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- Java for LeetCode 208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- leetcode@ [208] Implement Trie (Prefix Tree)
Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...
随机推荐
- UML图中聚合、组合、关联、依赖、泛化的强弱关系
一.泛化 1.说明 泛化是一种继承关系,如果一个类A的所有属性和操作能被另一个类B所继承,则类B不仅可以包含自己独有的属性,而且可以包含类A的属性和操作.继承是类与类或者类与接口之间最常见的关系. 2 ...
- IT痴汉的工作现状42-Android越用越慢?立场不同,谈何信任!
大海和我一次闲聊中问,你做安卓这么久.能给我解释一下为什么安卓手机用半年后就变慢吗? 呃... 伟仔还真是头一次听说这么精准的半年就慢的说法. 大海继续说."能不能是厂家有益设置的?哦,也不 ...
- struts2 18拦截器详解(九)
ScopedModelDrivenInterceptor 该拦截器处于defaultStack第八的位置,其主要功能是从指定的作用域内检索相应的model设置到Action中,该类中有三个相关的属性: ...
- Tushare数据的绘图操作
1.在代码里调试学习实在费劲,可以把数据取到df里,在交互界面里慢慢调试 2.柱状图 绘制柱状图,默认情况下乱,数据太密了 改用曲线图
- Syntax error missing ; before *
[问题] I have a header file like so: #pragma once #include "gamestate.h" #include "Ex ...
- 安装QT的时候出现PATH_MAX错误
运行c:\qt\4.5.0的configure文件的时候,出现如下的错误提示: ....\corelib\io\qfsfileengine_win.cpp(1012) : error C2065: ...
- 编程实战——电影管理器之利用MediaInfo获取高清视频文件的相关信息
随着高速(20M)宽带.HTPC.大容量硬盘(3T)的普及,下载高清片并利用大屏幕观看也成为普通的事情. 随着下载影片的增多,管理就有了问题,有时在茫茫文件夹下找寻一个影片也是一件费时费力的事. 于是 ...
- OpenGL ES 3.0 图元组合和光栅化(三)
图元是能够被OpenGL ES 绘制的几何物体,如三角形.线条或者精灵.在图元组合过程 中,对每个图元必须判断是否位于投影 截体内,如果图元不完全在平截体内部,将被视图平截体剪贴,如果完全在平截体外, ...
- Windows 10 中的 Shell 指令
这篇文章本无技术含量,也不高大上,不过想想,还是写下来吧,肯定有人会用得到的. 常玩系统的朋友,应该知道 Shell 指令,这是一个很好玩很神奇的指令.利用它,可以用简短的单词,轻易地打开系统中的一些 ...
- Linux netstat命令介绍
关注Linux的系统状态,主要从两个角度出发,一个角度是系统正在运行什么服务(ps命令):另外一个就是有什么连接或服务可用(netstat命令).netstat还可以显示ps无法显示的.从inetd或 ...