lintcode 中等题: 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
lintcode 中等题: Implement Trie的更多相关文章
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:A + B Problem A + B 问题
题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...
- lintcode 中等题:搜索旋转排序数组II
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
随机推荐
- 计算 sql查询语句所花时间
--1:下面这种是SQL Server中比较简单的查询SQL语句执行时间方法,通过查询前的时间和查询后的时间差来计算的: declare @begin_date datetimedeclare @en ...
- jquery 在页面中三种写法
jQuery 分 2 个系列版本 1.x 与 2.x,主要的区别在于 2.x 不再兼容 IE6.7.8浏览器,这样做的目的是为了兼容移动端开发.由于减少了一些代码,使得该版本比 jQuery 1.x ...
- php curl post
public static function curlPost($url, $data=array()) { $ch = curl_init(); if (is_array ...
- jQuery WIN 7透明弹出层效果
jQuery WIN 7透明弹出层效果,点击可以弹出一个透明层的jquery特效,插件可以调弹出框的宽度和高度,很不错的一个弹出层插件. 适用浏览器:IE8.360.FireFox.Chrome.Sa ...
- mysql-5.5.46源码编译安装
1.安装准备 cat /etc/redhat-release uname -r yum install ncurses-devel cmake automake autoconf make gcc g ...
- ASP.NET从数据库中取出数据,有数据的复选框为选中
在KS系统中在更新菜单的时候,当查出菜单的时候要查出菜单下面已经有了哪些界面了我用了一下的方法弄的.代码如下: 界面代码: <%@ Page Language="C#" Au ...
- 使用IO流创建文件并写入数据
/* 字符流和字节流: 字节流两个基类: InputStream OutputStream 字符流两个基类: Reader Writer 既然IO流是用于操作数据的, 那么数据的最常见体现形式是:文件 ...
- 【linux】
virtualbox hyper-v vmware KVM LXC Utils Docker
- 解决Maven默认仓库没有的jar下载(二)
前言: 在 “解决Maven不能下载“oracle.aspectjweaver.com.springsource.net.sf.cglib”jar(http://www.cnblogs.com/wql ...
- 【BZOJ】【2440】【中山市选2011】完全平方数
莫比乌斯函数/容斥原理 PoPoQQQ讲义引入例题= = 比较水……就是莫比乌斯函数的简单应用,也可理解为乱容斥一下…… 二分答案——>求1~x有多少个无平方因子的数Q(x). 引用一下PoPo ...