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树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
 
详细的可以看百度百科
 /**
* 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 val;
boolean isEnd;
int SIZE = 26;
TrieNode[] children; public TrieNode() {
children = new TrieNode[SIZE];
isEnd = false;
}
public TrieNode(char val) {
children = new TrieNode[SIZE];
this.val = val;
isEnd = false;
}
} public class Solution {
private TrieNode root; public Solution() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
if(word == null || word.length() == 0) return;
char[] cs = word.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++) {
int pos = cs[i] - 'a';
if(p.children[pos] == null) {
p.children[pos] = new TrieNode(cs[i]);
}
p = p.children[pos];
}
if(!p.isEnd) p.isEnd = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if(word == null || word.length() == 0) return false;
char[] cs = word.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++){
int pos = cs[i] - 'a';
if(p.children[pos] != null) {
p = p.children[pos];
}else return false;
}
return p.isEnd;
} // 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[] cs = prefix.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++){
int pos = cs[i] - 'a';
if( p.children[pos] != null) {
p = p.children[pos];
}else return false;
}
return true;
}
}

Implement Trie(LintCode)的更多相关文章

  1. 木材加工(LintCode)

    木材加工 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 样例 有3根木头[232 ...

  2. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  3. 判断数独是否合法(LintCode)

    判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填 ...

  4. 二进制求和(LintCode)

    二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 细节出了好多问题,提交了好多次... public class Solution { / ...

  5. leetcode 之Implement strStr()(27)

    字符串的匹配,返回匹配开始的位置,直接用暴力方式求解.为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可 直接停止匹配. char *strStr(char *haystack, ...

  6. 丑数(LintCode)

    丑数 设计一个算法,找出只含素因子3,5,7 的第 k大的数. 符合条件的数如:3,5,7,9,15...... 您在真实的面试中是否遇到过这个题? Yes 样例 如果k=4, 返回 9 挑战 要求时 ...

  7. Word Ladder(LintCode)

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  8. Container With Most Water(LintCode)

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  9. Single Number II(LintCode)

    Single Number II Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Examp ...

随机推荐

  1. 9.python爬虫--pyspider

    pyspider简介 PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项 ...

  2. [Luogu 2341] HAOI2006 受欢迎的牛

    [Luogu 2341] HAOI2006 受欢迎的牛 智能推的水题,一看是省选题就给做了,做一半才发现 Tarjan 算法忘干净了. Tarjan 求出SCC,算出每一个 SCC 包含原图的点数(s ...

  3. bzoj3043 IncDec Sequence

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3043 [题解] 比较神奇的一道题,开始没往差分的角度上想,所以没想出来. 考虑查分数组,有$ ...

  4. hibernate单列的多值查询

    比如你的表主键是id,你要删除id 是 34,56,99 这样的.. uid是拼好的 比如 '34','56','99' ,以前我会这样写 String queryString = "upd ...

  5. 每个 Java 开发者都应该知道的 5 个注解

    自 JDK5 推出以来,注解已成为Java生态系统不可缺少的一部分.虽然开发者为Java框架(例如Spring的@Autowired)开发了无数的自定义注解,但编译器认可的一些注解非常重要. 在本文中 ...

  6. 命令行创建KVM虚拟机

    qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G   qemu-system-x86_64 -m 2048 -enable-kvm ...

  7. xxx_initcall相关知识

    参考文件include/linux/init.h /* * Early initcalls run before initializing SMP. * * Only for built-in cod ...

  8. python之operator操作符函数

    operator函数主要分为以下几类:对象比较.逻辑比较.算术运算和序列操作. 举例: #python 3.4 >>> operator.eq(1,2)False >>& ...

  9. C# 数组 随机 排序

    ]; ; i < ; i++) { arrInt[i] = i; } arrInt = arrInt.OrderBy(c => Guid.NewGuid()).ToArray<int ...

  10. git - 开发者电脑与服务器的配置

    首先公司要有一台git服务器,现在一般都托管在github服务器上,在中国可能会托管到oschina上,oschina有一点好处就是可以免费托管私有项目,而在github上想要托管自己的项目是收费的, ...