实现一个 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. vue-cli3取掉eslint格式提示报错

    把package.json文件中关于eslint那个直接去掉. "@vue/cli-plugin-eslint": "^3.7.0",

  2. 精通CSS:高级Web标准解决方式(第2版)

    精通CSS:高级Web标准解决方式(第2版) 跳转至: 导航. 搜索 层叠重要度:(也就是说.用户!important能够覆盖inline style) !important.用户>作者.最后是 ...

  3. Make 编译脚本上手

    考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...

  4. WebService(2)-XML系列之用Stax操作Xml

    源代码下载:链接: http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:利用Stax处理xml文档 一.读取xml 1.基于光标的查找 核心:X ...

  5. 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例

    [iOS系列]-UIScrollView的介绍及结合UIPageControl实现图片播放的实例 第一:UIScrollView的常用属性 //表示UIScrollView内容的尺寸,滚动范围 @pr ...

  6. Thinking in Java -- 类型信息RTTI

    Thinking in Java – 类型信息 个人感觉 java 中的比較难的部分了,在看了些netty源代码发现事实上这块很实用. 这章重点是RTTI和反射.先说下自己的理解 RTTI是执行时识别 ...

  7. JavaScript对象(复习笔记)

    js对象 对象构造器 function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname ...

  8. md5 js

    js-md5 - npm https://www.npmjs.com/package/js-md5 var rotateLeft = function(lValue, iShiftBits) { re ...

  9. hosts所在文件夹以及***

    hosts所在文件夹: Windows 系统hosts位于 C:\Windows\System32\drivers\etc\hosts Android(安卓)系统hosts位于 /etc/hosts ...

  10. 设计模式-(14)装饰者模式 (swift版)

    一,概念 装饰者模式(Decorator):动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案. 多组合,少继承 二,UML图 抽象构件类(Compone ...