lintcode 中等题: Implement Trie
2015-11-06 20:32
197 查看
题目
样例
注意
You may assume that all inputs are consist of lowercase letters a-z.
解题
Trie,字典树,又称单词查找树、前缀树,是一种哈希树的变种。应用于字符串的统计与排序,经常被搜索引擎系统用于文本词频统计。
性质:
1.根节点不包含字符,除根节点外的每一个节点都只包含一个字符。
2.从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
3.每个节点的所有子节点包含的字符都不相同。
优点是查询快。对于长度为m的键值,最坏情况下只需花费O(m)的时间;而BST需要O(m log n)的时间。
程序来源链接
1.理解Trie 字典树很重要
2.定义TrieNode节点类很重要
孩子节点是HashMap的形式,可以很快速的取出其中的值。
上面理解了下面插入删除就容易了
Java Code
总耗时: 1599 ms
Implement Trie
Implement a trie with insert, search, and startsWith methods.样例
注意
You may assume that all inputs are consist of lowercase letters a-z.
解题
Trie,字典树,又称单词查找树、前缀树,是一种哈希树的变种。应用于字符串的统计与排序,经常被搜索引擎系统用于文本词频统计。
性质:
1.根节点不包含字符,除根节点外的每一个节点都只包含一个字符。
2.从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
3.每个节点的所有子节点包含的字符都不相同。
优点是查询快。对于长度为m的键值,最坏情况下只需花费O(m)的时间;而BST需要O(m log n)的时间。
程序来源链接
1.理解Trie 字典树很重要
2.定义TrieNode节点类很重要
class TrieNode { // Initialize your data structure here. char c; boolean leaf; HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); public TrieNode(char c) { this.c = c; } public TrieNode() {} }
孩子节点是HashMap的形式,可以很快速的取出其中的值。
上面理解了下面插入删除就容易了
/**
* 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 { // Initialize your data structure here. char c; boolean leaf; HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); public TrieNode(char c) { this.c = c; } public TrieNode() {} }
public class Solution {
private TrieNode root = null;
public Solution() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
Map<Character,TrieNode> children = root.children;
for(int i = 0;i< word.length() ;i++){
char c = word.charAt(i);
TrieNode t = null;
if(children.containsKey(c)){
t = children.get(c);
}else{
t = new TrieNode(c);
children.put(c,t);
}
children = t.children;
if(i == word.length() - 1)
t.leaf = true;
}
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode t = searchNode(word);
return t!=null && t.leaf;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
return searchNode(prefix) != null;
}
private TrieNode searchNode(String word){
Map<Character ,TrieNode> children = root.children;
TrieNode t = null;
for(int i = 0;i< word.length() ;i++){
char c = word.charAt(i);
if(!children.containsKey(c))
return null;
t = children.get(c);
children = t.children;
}
return t;
}
}
Java Code
总耗时: 1599 ms
相关文章推荐
- 像素翻转、清除行列、彻底搞懂int *a[10]和int (*a)[10]
- v5
- 【DP】 HDOJ 3507 Print Article
- C++模板库(包含C++11特性)
- HDU 4612——Warm up——————【边双连通分量、树的直径】
- YOU健康小助手
- 剑指Offer——链表中快行指针用法(链表中倒数第k个结点等)
- JavaScript设计模式 Item 7 --策略模式Strategy
- [TwistedFate]category Extensxion protocol
- Linux shell(1)
- mysql事务
- JavaScript设计模式 Item 7 --策略模式Strategy
- HDU2087-剪花布条-KMP模板
- 简易 SolidWorks的三维飞行器模型建立教程
- 调整数组使奇数全部都位于偶数前面
- redis3.0集群状态监控脚本用于zabbix监控需要python2.6
- SpringMVC 学习系列 (1) 之 初识SpringMVC
- Android——网络编程
- swift 2.0 语法 常量变量
- view,control,service,dao,model层的关系