实现一个 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) 字典树(前缀树)的更多相关文章

  1. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  2. LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are ...

  3. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  4. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  5. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  6. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  7. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. 208. Implement Trie (Prefix Tree) -- 键树

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

随机推荐

  1. SpringMVC+Hibernate+Junit4+json基本框架近乎0配置

    公司是做APP开发的,须要后台来提供接口,于是乎,这个任务就交给我,经过重复的尝试,学习和參考别人的demo,最终搭出自己还算惬意的框架.SpringMVC+Sping3+Hibernate4+Jun ...

  2. JavaSE入门学习9:Java基础语法之数组

    一数组的定义 数组能够理解为是一个巨大的"盒子",里面能够按顺序存放多个类型同样的数据.比方能够定义int型的数组 scores存储4名学生的成绩. watermark/2/tex ...

  3. 运算符与类型转换 mogondb简介

    运算符与类型转换   1.运算符 (1)分类 算术运算符.关系运算符.逻辑运算符.位运算符.赋值运算符.其他运算符 >.算术运算符: 运算符 描述 + 把两个操作数相加 - 从第一个操作数中减去 ...

  4. HTML5开发实战——Sencha Touch篇(1)

    学习了很多主要的Sencha Touch内容,已经了解了Sencha Touch的开发模式.接下来一段时间我们将利用Sencha Touch来进行自己的web应用构建. 先要解决的是前端的问题.从最简 ...

  5. sharpdevelop 调整代码字体显示大小

    SharpDevelop中使用ctrl+鼠标滚轮可以调整代码的字体显示大小

  6. Chapter1-data access reloaded:Entity Framework(上)

    本章包括以下几个部分: 1.DataSet and classic ADO.NET approach2.Object model approach3.Object/relational mismatc ...

  7. SpringMVC_架构 --跟海涛学SpringMVC(学习笔记)

    重点: 1.工作流程及实现原理 2.配置及使用方法 3.共同函数 前言 1.2.模型: 1.2.1.此处模型使用JavaBean,可能造成JavaBean组件类很庞大,一般现在项目都是采用三层架构,而 ...

  8. [m() for i in range(8)]

    import time def m(): print(time.time()) time.sleep(1) [m() for i in range(8)] 一行 list

  9. commons-fileupload、smartUpload和commons-net-ftp

    1.本地上传 在许多Web站点应用中都需要为用户提供通过浏览器上传文档资料的功能,例如,上传个人相片.共享资料等.在DRP中,就有这个一个功能,需要将对应的物料图片上传并显示.对于上传功能,其实在浏览 ...

  10. Android系统input按键处理流程(从驱动到framework)【转】

    本文转载自:http://blog.csdn.net/jwq2011/article/details/51234811 (暂时列出提纲,后续添加具体内容) 涉及到的几个文件: 1.out/target ...