LeetCode208: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.
Hide Tags Data Structure Trie
实现一棵Trie树以及实现查询的功能,依据上一篇文章中的分析和伪代码能够非常迅速地实现:
runtime:68ms
class TrieNode {
public:
// Initialize your data structure here.
TrieNode() {
words=0;
prefixs=0;
for(int i=0;i<26;i++)
edges[i]=NULL;
}
int words;
int prefixs;
TrieNode* edges[26];
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
insertHelper(root,word,0);
}
// Returns if the word is in the trie.
bool search(string word) {
return searchHelper(root,word,0)!=0;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
return startsWithHelper(root,prefix,0)!=0;
}
void insertHelper(TrieNode * node,string &word,int pos) {
if(pos==word.size())
{
node->words++;
node->prefixs++;
}
else
{
node->prefixs++;
int char_code=word[pos]-'a';
if(node->edges[char_code]==NULL)
node->edges[char_code]=new TrieNode();
insertHelper(node->edges[char_code],word,pos+1);
}
}
int searchHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->words;
else if(node->edges[char_code]==NULL)
return 0;
else
return searchHelper(node->edges[char_code],word,pos+1);
}
int startsWithHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->prefixs;
else if(node->edges[char_code]==NULL)
return 0;
else
return startsWithHelper(node->edges[char_code],word,pos+1);
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
LeetCode208:Implement Trie (Prefix Tree)的更多相关文章
- 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 ...
- Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 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 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. Example: ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- 附加数据库错误代码 - 5120【MSSQL】
解决方法 数据库所在的文件夹右击打开属性 - 安全 - 给予Authenticated Users用户完全控制权限.然后再附加一次即可成功.
- Manacher 学习笔记
\(\\\) \(Manacher\) 一种常用的字符串算法,用于处理一些回文字符相关的问题. 回文串:从前向后和从后向前输出一致. 回文中心:以这里开始,每次向外左右各扩展一个字符得到的回文串的中心 ...
- 谈一谈a:link、a:visited、a:hover、a:active的正确使用顺序
前端路上,未来还远,所以基础部分必须扎实,走好现在脚下的每一步才是现在最重要的. 下面进入正题吧. 1. <a>标签 我们先说一说<a>标签是干啥用的. <a> 标 ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 如何在redhat 7上安装VNC服务器
平时我们基本上都是用xshell或者用putty远程我们的linux服务器,如果我们的linux服务器安装了图型化界面那我们又该如何远程使用我们的图形化界面呢?下面我们用vnc来实现远程我们的linu ...
- Django settings.py的一些配置
官方文档:settings配置 静态文件配置链接 # 语言改为中文: LANGUAGE_CODE = "zh-hans" # 时区由UTC改为Asia/Shanghai,这样有关时 ...
- 1.2 Java“白皮书”的关键术语
Java的设计者已经编写了颇有影响力的“白皮书”,来解释设计的初衷以及完成的情况,并且发布了一个简短的摘要.这个摘要用下面11个关键术语进行组织: 简单性 面向对象 分布式 健壮性 安全性 体 ...
- springMVC+springJDBC+Msql注解模式
最近基于Spring4.X以上的版本写了一个springMVC+springJDBC+Msql注解模式的一个项目,之中也遇到过很多问题 ,为了防止以后遇到同样问题现记录一下知识点以及详细配置. 首先我 ...
- 清北学堂模拟赛d4t1 a
分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了. /* 1.没有发生改变, ...
- js es6 Object.freeze
将对象冻结,使用Object.freeze方法 const foo = Object.freeze({}); // 常规模式时,下面一行不起作用: // 严格模式时,该行会报错 foo.prop = ...