题目

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的更多相关文章

  1. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  2. lintcode 中等题:permutations II 重复数据的全排列

    题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...

  3. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  4. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  5. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  6. lintcode 中等题:A + B Problem A + B 问题

    题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...

  7. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

  8. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  9. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

随机推荐

  1. android-support-xxxx.jar NoClassDefFoundError

    当你的项目出现以下红色提示的时候,要小心了,因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the dep ...

  2. Silverlight动画学习笔记(三):缓动函数

    (一)定义: 缓动函数:可以将自定义算术公式应用于动画 (二)为什么要用缓动函数: 您可能希望某一对象逼真地弹回或其行为像弹簧一样.您可以使用关键帧动画甚至 From/To/By 动画来大致模拟这些效 ...

  3. 所有外包项目威客网站列表----来自程序员接私活网qxj.me

    猪八戒    http://www.zhubajie.com/  有佣金,建议别去坑死了 csto      http://www.csto.com/ 开源中国众包   https://zb.osch ...

  4. laravel--has方法--查看关联关系

    has()方法可以用来查询是否有关联关系的一个东西,一般其他的has方法 就是判断这个里面有没有值 $packageOrders = Company::has('packages')->get( ...

  5. PHP中数组排序实例学习

    先介绍下php中用于数组排序的函数: 排序方法                           升序                             降序                 ...

  6. mtu

    通信术语 最大传输单元(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接 ...

  7. 怎么删除有外键约束的MySQL表中的数据

    SET FOREIGN_KEY_CHECKS = 0 操作结束后 SET FOREIGN_KEY_CHECKS = 1

  8. WWDC 2016: Rich Notifications in iOS 10

    Notifications have gotten more than a visual refresh in iOS 10. As part of the new UserNotifications ...

  9. 如何利用OpenCV自带的级联分类器训练程序训练分类器

    介绍 使用级联分类器工作包括两个阶段:训练和检测. 检测部分在OpenCVobjdetect 模块的文档中有介绍,在那个文档中给出了一些级联分类器的基本介绍.当前的指南描述了如何训练分类器:准备训练数 ...

  10. Centos——rpm和yum

    间歇性的学习了centos的一些使用,发现一段时间不操作,就会忘掉其中的概念或者操作方式方法,于是在此总结一下. 一.问题描述 首先,把一个我最常忘记的概念性的东西在这里记录一下: 什么是yum,什么 ...