[LeetCode] 208. 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.
解法:
Trie(字典树)的知识参见:数据结构之Trie树 和 [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)。
可以采用数组和哈希表的方式实现,代码分别如下:
public 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) {
root.insert(word, 0);
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode result = root.search(word, 0);
return result != null && result.getIsWord();
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode result = root.search(prefix, 0);
return result != null;
}
}
class TrieNode {
private TrieNode[] children;
private boolean isWord;
public TrieNode() {
children = new TrieNode[26];
isWord = false;
}
public void insert(String word, int index) {
// 如果所有字符都已插入,需要将最后一个字符节点的isWord改为true
if (index == word.length()) {
this.isWord = true;
return;
}
// 如果不存在该字符,在对应位置新建节点
int n = word.charAt(index) - 'a';
if (children[n] == null) {
children[n] = new TrieNode();
}
// 继续下一字符
children[n].insert(word, index + 1);
}
// 由于Trie中既要求实现search,又要求实现startsWith,为了方便,
// 此处直接返回搜索结果的TrieNode,交由Trie去判断。
public TrieNode search(String word, int index) {
if (index == word.length()) {
return this;
}
int n = word.charAt(index) - 'a';
if (children[n] == null) {
return null;
}
return children[n].search(word, index + 1);
}
public boolean getIsWord() {
return this.isWord;
}
}
/**
* 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);
*/
public 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) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (!curr.children.containsKey(letter)) {
curr.children.put(letter, new TrieNode());
}
curr = curr.children.get(letter);
}
curr.isWord = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode result = find(word);
return result != null && result.isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode result = find(prefix);
return result != null;
}
public TrieNode find(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (!curr.children.containsKey(letter)) {
return null;
}
curr = curr.children.get(letter);
}
return curr;
}
}
class TrieNode {
HashMap<Character, TrieNode> children;
boolean isWord;
public TrieNode() {
children = new HashMap<>();
isWord = false;
}
}
/**
* 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);
*/
[LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆的更多相关文章
- 字典树(查找树) 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. Example: Trie trie = new Trie(); trie. ...
- Java for LeetCode 208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- leetcode@ [208] Implement Trie (Prefix Tree)
Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...
- 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)
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) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- 如何:通过将HTML编码应用于字符串来防止Web应用程序中的脚本漏洞
当用户可以将可执行代码(或脚本)添加到您的应用程序中时,会发生大多数脚本攻击.默认情况下,ASP.NET提供请求验证,如果表单发布包含任何HTML,则会引发错误. 您可以通过以下方式帮助防止脚本漏洞利 ...
- 结对作业-四则运算GUI
目录: 一.项目地址二.PSP三.接口设计四.计算模块接口的设计与实现过程五.计算模块接口部分的性能改进六.计算模块部分单元测试展示七.计算模块部分异常处理说明八.界面模块的详细设计过程九.界面模块与 ...
- 笔记:EF出现列名 'Discriminator' 无效、类没有加入数据库上下文也被数据迁移生成表
笔记: EF出现列名 'Discriminator' 无效: 类没有加入数据库上下文也被数据迁移生成表: 出现该问题一般是使用了某个基类继承了实体类: 原因是code first的POCO实体对象的继 ...
- 通过loadrunner将http返回response写入文本txt中
脚本如下 Action() { int myFile;//lr不支持FILE类型,所以定义一个int类型的file web_reg_save_param("goods_price" ...
- redis压力测试工具-----redis-benchmark
redis做压测可以用自带的redis-benchmark工具,使用简单 压测命令:redis-benchmark -h 127.0.0.1 -p 6379 -c 50 -n 10000 压测需要一段 ...
- C# 源码计数器
设计背景 编程工作中,有些文档需要填写代码量,例如申请软件著作权.查阅相关资料之后,编写了这个小程序. 设计思路 主要思路为分析项目文件,根据项目文件查找代码文件,然后遍历代码文件进行分析 相关技术 ...
- UVA11027_Palindromic Permutation
此题不错.给你一些字字符,要求你用这些字符构成一个回文串,求字典序第k大的回文串. 首先通过给定的字符,我们可以直接判断能否构成回文串(奇数的字符不超过一种),其次可以统计每个字符在回文串的左边应该出 ...
- Qt——用于表格QTableView的模型
如果想使用表格来呈现数据,Qt提供了一个方便的部件QTableWidget,但是直接用它实现一些功能可能比较困难.这里将介绍一种强大.灵活的方式来操作表格. 一.模型/视图架构 在这个架构中,模型用于 ...
- DAY1-Flask项目
1.pipenv:与virtualenv类似的第三方的Python运行虚拟环境 给每个项目安装pipenv环境:pipenv install 启动:pipenv shell 使用pipenv安装Fla ...
- 51nod 1589 移数博弈 | 基数排序(ノಠ益ಠ)ノ彡┻━┻
51nod 1589 移数博弈 题面 给出一个序列a,长度 n <= 10^7, a[i] <= 10^7 求每个长度 >= 2 的区间的最大值*次大值 之和. 题解 主要思路是求每 ...