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. android设计准则

    ------------缘由-------------------------------------------------------------------------------------- ...

  2. UIImageView与UIScrollView的关系图

        UIImageView与UIScrollView的关系图           https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...

  3. Vuejs - 单文件组件

    为什么需要单文件组件 在之前的实例中,我们都是通过 Vue.component 或者 components 属性的方式来定义组件,这种方式在很多中小规模的项目中还好,但在复杂的项目中,下面这些缺点就非 ...

  4. 爬虫--requests讲解

    什么是requests? Requests是用Python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全 ...

  5. bzoj 1054 bfs

    就是bfs,对于每个状态存一个hash为当前状态矩阵的二进制表示,然后搜就行了,写成双向bfs会快很多. 反思:对于C++的数组从0开始还不是特别习惯,经常犯错,对于C++的结构体不熟. /***** ...

  6. selenium遇到的一些问题,持续更新

    1.今天早上运行程序的时候,发现我在循环点击一个元素的时候出现了错误 selenium.common.exceptions.StaleElementReferenceException: Messag ...

  7. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...

  8. python 使用国内源安装软件

    python linux 等 使用国内源安装软件 速度更快 你值得拥有 ! 豆瓣源:pip install -i https://pypi.douban.com/simple/ 阿里源:pip ins ...

  9. 数据库简述(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除和修改.可以说,数据库是长期存储在计算机 ...

  10. jps命令学习

    jps命令学习 标签(空格分隔): jvm jps介绍 ( JVM Process Status Tool ) 网文 jps命令用于查看当前Java进程及其pid等相关信息,同ps -ef | gre ...