Implement Trie (Prefix Tree) 解答
Question
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
Solution
A trie node should contains the character, its children and the flag that marks if it is a leaf node.
Trie is an efficient information retrieval data structure. Using trie, search complexities can be brought to optimal limit (key length).
Every node of trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key as leaf node.
We design TrieNode to store 1. value 2. isLeaf 3. a map of children (map is used for quick selection)
In this way, we can implement Trie start from root. Add new child to original parent is implemented by put action in map.
Note here, search and startsWith differs.
null
|
a
|
b
for "a", search will return false, while startsWith will return true.
/**
* Your Trie object will be instantiated and called as such:
* Trie trie = new Trie();
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
class TrieNode {
char val;
boolean isLast;
Map<Character, TrieNode> children;
// Initialize your data structure here.
public TrieNode(char val) {
children = new HashMap<Character, TrieNode>();
this.val = val;
this.isLast = false;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode(' ');
} // Inserts a word into the trie.
public void insert(String word) {
if (word == null || word.length() == 0) {
return;
}
TrieNode p = this.root;
char[] arr = word.toCharArray();
for (char cur : arr) {
if (!p.children.containsKey(cur)) {
p.children.put(cur, new TrieNode(cur));
}
p = p.children.get(cur);
}
p.isLast = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
char[] arr = word.toCharArray();
TrieNode p = this.root;
for (char cur : arr) {
Map<Character, TrieNode> children = p.children;
if (!children.containsKey(cur)) {
return false;
}
p = children.get(cur);
}
return p.isLast;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() == 0) {
return false;
}
char[] arr = prefix.toCharArray();
TrieNode p = this.root;
for (char cur : arr) {
Map<Character, TrieNode> children = p.children;
if (!children.containsKey(cur)) {
return false;
}
p = children.get(cur);
}
return true;
}
}
Implement Trie (Prefix Tree) 解答的更多相关文章
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 【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 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)空间复杂度:字典树远远小于哈 ...
- 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. Example: ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- java与.net比较学习系列(1) 开发环境和常用调试技巧
最近因为公司项目要由.net平台转到java平台的原因,之前一直用.net的我不得不开始学习java了,刚开始听到说要转java的时候很抗拒,因为我想专注在.net平台上,不过这样也并不完全是坏事,通 ...
- 泛泰A880S升级官方4.4.2 binx教程
之前为了尝鲜,直接官升4.4.2,红砖了.越南兄弟说官方没更新升级工具,所以导致升级到78%就停止了,他给的办法,我试也没成功.官方旧版本号又不能升级S0221118以上的版本号,新版的离线升级工具没 ...
- mmc加工配套问题
题目如下,本题还有其它解.
- iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用
iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...
- 2016-XCTF Final-Richman
抽时间将XCTF Final中Richman这个题总结了下.题目及ida idb所在的链接在:http://files.cnblogs.com/files/wangaohui/richman-blog ...
- gradient杂谈
工作中难免遇到某些小项目中没有设计的情况,这对于PS基础薄弱的我来说非常恐怖.无奈之下,只好自己自学UI方面的知识,但对于某些能用CSS实现的背景样式等,还是尽可能地用已经掌握的知识去实现.本文主要分 ...
- 改进的简单Tooltips显示
使用js简单改进了Tooltips的显示效果,可进一步使用CSS对改进的Tooltips进行美化. 前台布局代码: <asp:Panel ID="Panel1" runat= ...
- React-Native OpenGL体验一
昨天初体验了一把SVG一个并不是多么复杂的动画,我在iOS模拟器上体验的是流畅的,但是在Android真机上体验,还是比较卡的. 下面来介绍一个OpenGL的第三方库: 下面是我运行的里面Demo的效 ...
- [Head First Python]2. BIF(内置函数)
1- range() >>> for each in range(2): print(each) ... 0 1
- regular expression 基本语法
正则检测网址: http://regexpal.isbadguy.com/ (不包含界定符) 或 http://tool.chinaz.com/regex unicode转换工具 : htt ...