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.
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:


在node数组中选取一个node点来插入
[一刷]:
- 搜索是在node上进行的,结果也应该保存在一个node中,用来处理返回值
- insert的特判是index满了,此时无法插入;不是空,空时正常插入。find的特判是满了,此时就是返回自身,不是空,因为children[pos]为空就是普通情况。
- 寻找的index == word.length()时,直接返回单词本身。
[二刷]:
- children[pos]装的就是TrieNode,没有时 需要变成一个同类型的节点,稍微理解下
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
感觉难是因为java基础不扎实,多做几次吧。
- insert和find都是在中间的拐点处进行的 而非从起点开始,有一般性。没见过
- TrieNode中对children[pos]操作,Trie中对root进行操作
[复杂度]:Time complexity: O(n) Space complexity: O(<n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- trie的时间复杂度和hashmap一样。优点在于空间复杂度,开头相同的字符串,不必重复存。
- 声明成员变量、引用在方法外部,自定义的类中要有方法实例化自己 新建对象,在方法内部,后期才能用this调用。这是java的基础知识,之前没有概念。
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
211. Add and Search Word - Data structure design 就是写类,用数据结构辅助完成
[代码风格] :
class TrieNode {
//data structure
private TrieNode[] children;
public boolean hasWord;
//initialization
public TrieNode() {
children = new TrieNode[26];
hasWord = false;
}
//insert
public void insert(String word, int index) {
if (index == word.length()) {
this.hasWord = true;
return ;
}
int pos = word.charAt(index) - 'a';
if (children[pos] == null) {
children[pos] = new TrieNode();
}
children[pos].insert(word, index + 1);
}
//find
public TrieNode find(String word, int index) {
if (index == word.length()) {
return this;//this
}
int pos = word.charAt(index) - 'a';
if (children[pos] == null) {
return null;
}
return children[pos].find(word, index + 1);//to who
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
/*
* @param word: a word
* @return: nothing
*/
public void insert(String word) {
root.insert(word, 0);
}
/*
* @param word: A string
* @return: if the word is in the trie.
*/
public boolean search(String word) {
TrieNode node = root.find(word, 0);//to who
return ((node != null) && (node.hasWord));
}
/*
* @param prefix: A string
* @return: if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
TrieNode node = root.find(prefix, 0);//to who
return (node != null);
}
}
Implement Trie (Prefix Tree)实现字典树的更多相关文章
- [LeetCode] 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 a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- [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 ...
- 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 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 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- react中父组件调用子组件的方法
1.直接使用ref进行获取 import React, {Component} from 'react'; export default class Parent extends Component ...
- JS数组中级+高级技巧
本文介绍JS数组一些比较进阶的方法: reverse:数组反转: join:(参数)以参数为连接符将数组拼接为字符串: 实例: var arr=[]; arr[3]="haha"; ...
- 故障处理:磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1
按照阿里云官网教程对云服务器进行磁盘扩容,使用fdisk重新分区,最后使用e2fsck和resize2fs来完成文件系统层面的扩容 在执行“e2fsck -f /dev/vdb1”命令时报错,如果你的 ...
- BZOJ3033: 太鼓达人(欧拉回路)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 524 Solved: 400[Submit][Status][Discuss] Description ...
- 华为交换机STP 根ID优先级设置
http://m.blog.csdn.net/flyfish5/article/details/50224537 STP(Spanning Tree Protocol)生成树协议. 冗余链路 当前的交 ...
- php 自定义函数大全
1. call_user_func和call_user_func_array 以上两个函数以不同的参数形式调用函数.见如下示例: <?php class demo{ public static ...
- js基础篇string&&array(应YX同学面试复习要求 - -)
js中的数据类型一共有五个基本数据类型,分别是undefined,null,boolean,number,string. js中的Object类型中包括两大类型:Function类型和array类型. ...
- make 写法练习
cc=g++ all:signal %:%.o $(cc) -o $< $@ %.cpp:%.o echo se $< $@ $* $^ g++ -c $< $@cl: rm -rf ...
- 让多个Fragment 切换时不重新实例化
转自:http://www.yrom.net/blog/2013/03/10/fragment-switch-not-restart/ 让多个Fragment 切换时不重新实例化 在项目中需要进行Fr ...
- Android网络请求
HTTP请求与响应 HTTP请求包结构 例: POST /meme.php/home/user/login HTTP/1.1 Host: 114.215.86.90 Cache-Control: no ...