208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法。
注意:
你可以假设所有的输入都是小写字母 a-z。
详见:https://leetcode.com/problems/implement-trie-prefix-tree/description/
Java实现:
Trie树,又称为字典树、单词查找树或者前缀树,是一种用于快速检索的多叉数结构。例如,英文字母的字典树是26叉数,数字的字典树是10叉树。
Trie树的基本性质有三点,归纳为:
根节点不包含字符,根节点外每一个节点都只包含一个字符。
从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
每个节点的所有子节点包含的字符串不相同。
insert:分析单词的每一个字符,如果存在与已经有的孩子中,则循环转到该孩子节点,否则新建孩子节点,最后在单词的末尾字符isWord标志位true;
search: 逐个分析每个字符,如果不存在该字符的孩子则返回false,否则循环之后,查看末尾字符的标志位即可;
prefix: 和search一样,只是在最后只要是都存在每个字符相应的孩子map,则返回true。
class TrieNode{
boolean isWord;
HashMap<Character,TrieNode> map;
public TrieNode(){
map=new HashMap<Character,TrieNode>();
}
}
class Trie {
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root=new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
char[] charArray=word.toCharArray();
TrieNode tmp=root;
for(int i=0;i<charArray.length;++i){
if(!tmp.map.containsKey(charArray[i])){
tmp.map.put(charArray[i],new TrieNode());//添加
}
tmp=tmp.map.get(charArray[i]);//转到孩子节点
if(i==charArray.length-1){//末尾字符
tmp.isWord=true;
}
}
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode tmp=root;
for(int i=0;i<word.length();++i){
TrieNode next=tmp.map.get(word.charAt(i));
if(next==null){
return false;
}
tmp=next;
}
return tmp.isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode tmp=root;
for(int i=0;i<prefix.length();++i){
TrieNode next=tmp.map.get(prefix.charAt(i));
if(next==null){
return false;
}
tmp=next;
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
C++实现:
class TrieNode
{
public:
TrieNode *next[26];
char c;
bool isWord;
TrieNode():isWord(false)
{
memset(next,0,sizeof(TrieNode*)*26);
}
TrieNode(char _c):c(_c),isWord(false)
{
memset(next,0,sizeof(TrieNode*)*26);
}
};
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root=new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode *p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
{
p->next[id]=new TrieNode(c);
}
p=p->next[id];
}
p->isWord=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode *p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
{
return false;
}
p=p->next[id];
}
return p->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode *p=root;
int id;
for(char c:prefix)
{
id=c-'a';
if(p->next[id]==nullptr)
{
return false;
}
p=p->next[id];
}
return true;
}
private:
TrieNode *root;
}; /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/
208 Implement Trie (Prefix Tree) 字典树(前缀树)的更多相关文章
- 【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 a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are ...
- 字典树(查找树) 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 a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 【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: ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 208. Implement Trie (Prefix Tree) -- 键树
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
随机推荐
- 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘
[C#]无损转换Image为Icon 如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle ...
- 一颗ARM架构芯片的软硬件构成
硬件和软件是一颗芯片系统互相依存的两大部分.本文总结了一颗芯片的软硬件组成.作为对芯片的入门级概括吧. (一)硬件 主控CPU:运算和控制核心.基带芯片基本构架採用微处理器+数字信号处理器(DSP)的 ...
- CodeForces 567C. Geometric Progression(map 数学啊)
题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...
- Android Material Design-Maintaining Compatibility(保持兼容性)-(七)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40634829 翻译自: http://developer.android.com/traini ...
- 前端富文本 js 版本
https://s3.pstatp.com/pgc/v2/resource/tt_ueditor_v3_temple/tt-editor.all.js?20180425
- http://www.freetds.org/userguide/what.htm
FreeTDS is re-implementation of C libraries originally marketed by Sybase and Microsoft SQL Server. ...
- CRM IFD 部署在同一台服务器上遇到的错误
为了学习Dynamics 365,在阿里去上部署一台Dynamics 365服务器.然后实现了IFD 部署.学习的路线还之前的CRM 版本大致一样,这里只是记录一下我遇到的问题及解决问题的思路. 学习 ...
- Package vim is not available, but is referred to by another package及我的vim配置
新安装的ubuntu,先安装vim,但是安装出现 Reading package lists... Done Building dependency tree Reading state inform ...
- Rust语言——无虚拟机、无垃圾收集器、无运行时、无空指针/野指针/内存越界/缓冲区溢出/段错误、无数据竞争
2006年,编程语言工程师Graydon Hoare利用业余时间启动了Rust语言项目.该项目充分借鉴了C/C++/Java/Python等语言的经验,试图在保持良好性能的同时,克服以往编程语言所存在 ...
- BZOJ1143:祭祀river(二分图求有向图的最大点独立集)
在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河道连接着 ...