[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 ...
随机推荐
- 项目Beta冲刺(团队)总结
团队成员及分工 姓名 学号 分工 陈家权 031502107 前端(消息模块) 赖晓连 031502118 前端(问答模块) 雷晶 031502119 服务器 林巧娜 031502125 前端(首页模 ...
- “吃神么,买神么”的第一个Sprint计划(第六天)
“吃神么,买神么”项目Sprint计划 ——5.26 星期二(第六天)立会内容与进度 摘要:logo最终出来了,,背景也出来了,可以开始将完成的部分放到同一个文件中,决定剩下的时间把昨晚的部分贴上去 ...
- p4factory下 targets/basic_rout
p4factory/targets/basic_routing/p4src代码解读 headers.p4 header_type ethernet_t { fields { dstAddr : 48; ...
- iOS-UICollectionViewLayout方法介绍
注意:UICollectionView的自定义功能就是自己去实现UICollectionViewLayout的子类,然后重写相应的方法来实现Cell的布局 1.当布局首次被加载时会调用prepareL ...
- beta 发布的相关评论
1. 礼物挑选小工具 飞天小女警 这个项目的创意独具匠心,贴近实际,令人耳目一新,网站的页面也是玫红色的,配色让人感到很温馨,对礼物的筛选方式很有趣,使用的记录特殊日子的方法来提醒自己挑选礼 ...
- Python入门:条件控制
条件控制其实就是if...else...(如果...条件是成立的,就做...:反之,就做...)的使用,其基本结构是: 具体看下面这个例子: def account_login(): # 定义函数 p ...
- HDU 2123 An easy problem
http://acm.hdu.edu.cn/showproblem.php?pid=2123 Problem Description In this problem you need to make ...
- HDU 2140 Michael Scofield's letter
http://acm.hdu.edu.cn/showproblem.php?pid=2140 Problem Description I believe many people are the fan ...
- [转帖]HTTPS的简单说明
HTTPS(全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,简单讲是 HTTP 的安全版,即 HTT ...
- [转帖]HTTPS系列干货(一):HTTPS 原理详解
HTTPS系列干货(一):HTTPS 原理详解 https://tech.upyun.com/article/192/HTTPS%E7%B3%BB%E5%88%97%E5%B9%B2%E8%B4%A7 ...