leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree)
1 题目
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
2 思路
该题是实现trie树。
Trie,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
它有3个基本性质:
- 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
- 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
- 每个节点的所有子节点包含的字符都不相同。
此外,每个节点存在一个判断其是否是一个单词结尾的标记,用来表明该trie树中包含该单词。因此我们首先需要定义节点的数据结构。
节点的属性主要包含以下几点:
char content:节点字符内容boolean isEnd:节点是否为一个单词结束的标记LinkedList<TrieNode> childNode: 该节点的所有的孩子节点

3 代码
TrieNode
import java.util.LinkedList;
public class TrieNode {
// Initialize your data structure here.
char content; // 节点内容
boolean isEnd;// 是否为一个单词的结尾
LinkedList<TrieNode> childNode;// 该节点所有的孩子节点
// Initialize your data structure here.
public TrieNode() {
this.content = 0;
this.isEnd = false;
this.childNode = new LinkedList<TrieNode>();
}
public TrieNode(char content) {
this.content = content;
this.isEnd = false;
this.childNode = new LinkedList<TrieNode>();
}
public TrieNode subNode(char c) {
for (TrieNode tn : childNode) {
if (tn.content == c) {
return tn;
}
}
return null;
}
@Override
public String toString() {
return ("content:" + content + "\n child:" + childNode.toString());
}
}
Trie
public class Trie {
// 字典树的实现,效率高于HashMap
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
if (search(word)) {
return;
}
int len = word.length();
TrieNode cur = root;
for (int i = 0; i < len; i++) {
char c = word.charAt(i);
TrieNode tmp = cur.subNode(c);
if (tmp == null) {
tmp = new TrieNode(c);
cur.childNode.add(tmp);
cur = tmp;
} else {
cur = tmp;
}
}
cur.isEnd = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
int len = word.length();
TrieNode cur = root;
for (int i = 0; i < len; i++) {
char c = word.charAt(i);
TrieNode tmp = cur.subNode(c);
if (tmp == null) {
return false;
} else {
cur = tmp;
}
}
return cur.isEnd;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
int len = prefix.length();
TrieNode cur = root;
for (int i = 0; i < len; i++) {
char c = prefix.charAt(i);
TrieNode tmp = cur.subNode(c);
if (tmp == null) {
return false;
} else {
cur = tmp;
}
}
return true;
}
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("somestring");
trie.insert("key");
boolean res = trie.startsWith("ke");
System.out.println(res);
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
4 总结
很不错的问题,估计面试网易有道,360,百度这样的企业会考到。
leetcode面试准备:Implement Trie (Prefix Tree)的更多相关文章
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- 【LeetCode 208】Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
随机推荐
- C# string.Format格式化时间或货币
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- selinux理解1-selinux介绍
安全增强式Linux(SELinux, Security-Enhanced Linux)是一种强制访问控制(mandatory access control)的实现.它的作法是以最小权限原则(prin ...
- 使用info.plist(或工程名-info.plist)向程序中添加软件Build ID或者版本号信息
在实际应用程序开发过程中,经常需要向程序中添加软件版本号或者类似的信息,以保证之后发现问题时知道bug所在的版本,我们可以通过在工程名-info.plist文件中设置相关的key/value对(键/值 ...
- c#解析Josn(解析多个子集,数据,可解析无限级json)
首先引用 解析类库 using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...
- Mysql的联合查询
联合查询 union联合查询 语法: select 语句 union [all|distinct] select 语句; 示例: 特点: 条件:两表的查询出来的字段数目必须相同 ...
- 03_HttpClient_Post请求
[实例1.最最最简洁的POST请求] @Test public void test1() throws Exception{ //1.创建Htpclient实例(可关闭 Closeable) Clos ...
- android开发之GenyMotion与intelliJ的配置
(注意:这是在你的电脑上安装了intelliJ和安卓SDK后才进行的工作,如果没有intelliJ和安卓SDK,请先安装以上两样东西) 号称史上最快乐的模拟器GenyMotion,试一下. 第一步:下 ...
- ThinkPHP3.2 加载过程(三)
上次回顾: IS_CGI ,IS_WIN,IS_CLI,MAGIC_QUOTES_GPC干嘛用 IS_WIN 看了一下后面的代码 基本上就是为了保证在不同环境下运行时,由于有些操作系统会对文件路径大 ...
- RabbitMQ远程访问配置
1 首先创建一个新的账户 并给上Administrator标签 2然后给这个新账户添加虚拟主机访问权限 3在windows 下的 rabbitmq安装文件下的etc文件下的配置文件添加以下 [ ...
- AngularJS(1)随笔
ng-app 指令告诉 AngularJS,<div> 元素是 AngularJS 应用程序 的"所有者". ng-model 指令把输入域的值绑定到应用程序变量 na ...