Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
示例:
Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // 返回 true trie.search("app"); // 返回 false trie.startsWith("app"); // 返回 true trie.insert("app"); trie.search("app"); // 返回 true
说明:
- 你可以假设所有的输入都是由小写字母 a-z 构成的。
- 保证所有输入均为非空字符串。
前缀树的基础原理实现
class Trie {
public:
struct TrieNode
{
TrieNode() : isword(false), children(26, nullptr){}
~TrieNode()
{
for(TrieNode* child : children)
{
if(child)
delete child;
}
}
bool isword;
vector<TrieNode*> children;
};
/** Initialize your data structure here. */
Trie() : root(new TrieNode())
{
}
TrieNode* root;
const TrieNode* Find(const string &prefix) const
{
const TrieNode* newRoot = root;
for(const char c : prefix)
{
newRoot = newRoot ->children[c - 'a'];
if(newRoot == nullptr)
break;
}
return newRoot;
}
/** Inserts a word into the trie. */
void insert(string word)
{
TrieNode* newRoot = root;
for(char c : word)
{
if(newRoot ->children[c - 'a'] == nullptr)
{
newRoot ->children[c - 'a'] = new TrieNode();
}
newRoot = newRoot ->children[c - 'a'];
}
newRoot ->isword = true;
}
/** Returns if the word is in the trie. */
bool search(string word)
{
const TrieNode* tmp = Find(word);
return tmp != nullptr && tmp ->isword == true;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix)
{
return Find(prefix) != nullptr;
}
};
Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)的更多相关文章
- 【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 ...
- 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) ☆☆☆
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面试准备: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 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: ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
随机推荐
- 如何设置linux启动过程中的停止阶段
设置方法: 1 启动过程中点击“e”键(fedora)或者"tab"(centOS)键进入目标启动项的命令行参数下,移除initrd所在行末尾的"quiet" ...
- ios网络学习------2 用非代理方法实现同步post请求
#pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark post登录方法 -(void) ...
- ArcGis拓扑——规则、概念与要点
在地理数据库中,拓扑是定义点要素.线要素以及面要素共享重叠几何的方式的排列布置.例如,街道中心线与人口普查区块共享公共几何,相邻的土壤面共享公共边界. 处理拓扑不仅仅是提供一个数据存储机制.在 Arc ...
- wpf中datagrid绑定数据源发生改变
1.若datagrid绑定的数据源是同一个的话,即使里面的数据不同.页面也不会刷新,则需要重置数据源,再绑定.处理如下: datagrid1.ItemsSource=ListModule; 若List ...
- 四.python注释说明
Python第四节 Python注释 注释说明 注释分为单行注释和多行注释 单行注释以#开头 # 注释示例 > print("上面是一个注释的示例") 多行注释 多行注释可以 ...
- 内网端口转发[netsh]
一.利用场景 当前获取目标内网边界区域一台机器,可以通外网和内网也就是存在两块网卡,又通过其他手段获取到内网另外一台机器,但是这台机器不能出外网,所以我们可以使用windows自带netsh命令通过边 ...
- wbinfo - 向winbind服务查询信息
总览 SYNOPSIS wbinfo [-a user%password] [-c username] [-C groupname] [--domain domain] [-I ip] [-s sid ...
- 随笔记录 误删boot恢复 2019.8.7
系统还原: 1. 2. 3. 4. 5.进入硬盘 6.挂载光盘,安装恢复boot 7.安装grub2 8.重建grub.cfg文件
- Struts2.xml的配置
框架初始知识: Servlet VS Filter Filter的性能更强,因为Servlet能实现的,Filter都能实现. Filter还有拦截资源的作用 是Servlet所不能实现的. F ...
- Jenkins配置gitlab
一.免密公钥登陆1 登陆gitlab 搜ssh Keys 2 添加在Jenkins 服务器本地创建好的公钥 保存完成 也可以手动添加 到/var/opt/gitlab/.ssh/authorized_ ...