LeetCode——Implement Trie (Prefix Tree)
Description:
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
实现一个简单的Trie树,参考我的博客:http://www.cnblogs.com/wxisme/p/4876197.html
class TrieNode {
public TrieNode[] son;
public boolean isEnd;
// Initialize your data structure here.
public TrieNode() {
this.son = new TrieNode[26];
isEnd = false;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
char[] wordChars = word.toCharArray();
TrieNode node = this.root;
for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
node.son[pos] = new TrieNode();
}
node = node.son[pos];
}
node.isEnd = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
char[] wordChars = word.toCharArray();
TrieNode node = this.root;
for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return false;
}
node = node.son[pos];
}
return node.isEnd;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
char[] prefixChars = prefix.toCharArray();
TrieNode node = this.root;
for(char ch : prefixChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return false;
}
node = node.son[pos];
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
LeetCode——Implement Trie (Prefix Tree)的更多相关文章
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- (medium)LeetCode .Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
- 最近输入法的问题:关于ctrl + space 无法开关闭输入法的问题
输入法无法切换最好解决方法首先任务栏上的输入法图标上点右键选择设置. 然后选择键设置,双击第一个“在不同的输入语言之间切换”先勾选“切换输入语言”下面选择左手ALT.取消右边“切换键盘布局”前的勾. ...
- UITableView 顶部能够放大的图片
UITableView 顶部能够放大的图片 现在有挺多的应用在 UITableView 顶部加入图片,通过拖拽 UITableView 来实现图片的放大. 对比一下腾讯出品的两款App QQ:可展示更 ...
- 树莓派命令行配置连接wifi
iwlist scan sudovim /etc/wpa_supplicant/wpa_supplicant.conf network={ ssid="WIFINAME" ...
- Maven Missing artifact jar
maven error:Multiple annotations found at this line: - Missing artifact log4j:log4j:jar:1.2.15:compi ...
- 【ML】数据集资源
http://www.kdnuggets.com/datasets/index.html http://kdd.ics.uci.edu/
- 本地文件到通过flume到hdfs
配置文件 cd /usr/app/flume1.6/conf vi flume-dirTohdfs.properties #agent1 name agent1.sources=source1 age ...
- mysql查询某个库的表个数
SELECT COUNT(1) FROM information_schema.tables WHERE table_schema = 'leleli'; --解释:数据库名叫“leleli”
- mysql中如何删除表上的索引?删除索引?
需求描述: 今天在做SQL的优化的时候,想要把mysql中某个表上的索引删除掉,突然忘记语法了,找到帮助,在此记录下 操作过程: 1.查看表上的索引 show index from ti_o_sms; ...
- Git学习笔记(三)
Git提交相关内容 在Git提交时,会保存一个提交对象,该对象包括一个指向暂存区内容快照的指针,包括本次提交作者等相关附属信息,包括零个或多个指向该提交对象的父对象指针:首次提交时是没有祖先,普通提交 ...
- Git Step by Step – (4) 探索.git目录
前面一篇文章介绍了Git对象模型,接下来我们就进入".git"目录看看到底有什么东西,目录中哪些东西又跟Git对象模型相关.结合这个目录,我们将进一步了解Git的工作原理. .gi ...