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 ...
随机推荐
- 【转】ODBC和OLEDB的连接字符串
ODBC连接 适合数据库类型 连接方式access "Driver={microsoft access driver(*.mdb)};dbq=*.mdb;uid=admin;pwd=pas ...
- mybatis 无法转换为内部表示 解决
出现这个问题,一般是JavaBean定义的时候数据类型不准造成的. 其中javabean定义的字段的类型并不需要完全和数据库中的字段一样. 在mapper.xml中可能做了转换 比如 CASE WH ...
- Swift 2.0基本语法
内容包括:01变量&常量 02分支 03循环 04字符串 05数组 06字典 07函数 01变量&常量 //: Playground - noun: a place where peo ...
- javascript 第26节 jQuery对象
<html> <head> <title>jQuery</title> <!--导入jquery库--> <script type=& ...
- 九度OJ 1387 斐波那契数列
题目地址:http://ac.jobdu.com/problem.php?pid=1387 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义 ...
- OpenJudg / Poj 1363 Rails
1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...
- Eclipse启动报错Java was started but returned exit code=13
启动Eclipse的时候报错Java was started but returned exit code=13,这个错误的原因是由于eclipse版本与jdk版本不符导致的,可能你的eclipse是 ...
- echshop 微信扫码支付 遇到的问题
参考的网站 (转)http://www.ecshop119.com/ecshopjc-937.html(转)http://www.6gdown.com/softedupage/58929.html ...
- Java Web开发之详解JSP
JSP作为Java Web开发中比较重要的技术,一般当作视图(View)的技术所使用,即用来展现页面.Servlet由于其本身不适合作为表现层技术,所以一般被当作控制器(Controller)所使用, ...
- 即时Web通信总结
即时Web通信在一些对数据实时性要求特别严格的应用中十分重要,如监控系统.报价系统.股票交易系统和即时在线聊天应用等,由于http协议设计当初是为了服务器端响应客户端的请求而设计的,只能在客户端主动发 ...