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 ...
随机推荐
- eclipse中svn提交报错的解决
- 【转帖】如何在redhat单机服务器上运行postgresql的多个实例(howto run multiple postgresql instance on one redhat server)
Running multiple PostgreSQL 9.2 Instances on one server in CentOS 6/RHEL 6/Fedora 原帖网站速度很慢,故转帖在此 Thi ...
- 1. 自动化运维系列之Cobbler自动装机
preface 我们之前批量安装操作系统的时候都是采用pxe来安装,pxe也是通过网络安装操作系统的,但是PXE依赖于DHCP,HTTP/TFTP,kicstart等支持.安装流程如下所示: 对于上面 ...
- 5 云计算系列之glance镜像服务安装
preface 在上节中我们了解了keystone服务,下面就看看glance管理镜像的服务吧. glance组成 glance有两部分组成: glance-api 接受云系统镜像的创建,删除,读取请 ...
- php json_encode 返回false
今天用ajax请求不到数据,发现问题出现在最后一句 echo json_encode($data); var_dump(json_encode($data));输出false 使用json_last_ ...
- Android开发学习笔记-自定义控件的属性
若想让自定义控件变得更加方便灵活,则就需要对控件进行定义属性,使其用起来更方便. 下面是自定义控件属性的方法 1.添加attrs.xml,内容格式样式可以参考sdk\platforms\android ...
- VS2008 远程调试器未成功安装,没法使用?
Win7 64位系统,安装VS2008后,想使用远程调试功能,结果不能使用! 在VS2008的安装包下有个 Remote Debugger 的文件夹,在里面找到 x64 的 rdbgsetup.exe ...
- 五步整理你的css文件
鉴于实在无法忍受那种写一句就换一行的css写法,有个项目中的一个css文件竟然高达6000多行,看着实在蛋疼,无实今天下定决心整理一下,在DW里可以用正则很好的进行替换,步骤如下: 一:\r => ...
- javascript提取联通个人信息和通话记录的代码
由于一些巨大的困难,一些后端爬虫改成了前端爬虫. 前端爬虫是只有js语言,后端爬虫有python java nodejs php这些语言. 前端爬虫有window.document对象,在浏览器端的爬 ...
- [hadoop] hdfs 格式化错误 java.net.UnknownHostException
执行 hdfs namenode -format 抛出错误 主机名称异常,查看主机信息 原来 通过 bogon 无法找到主机 ,在host 中也没有对应的映射信息 修改后即可 再次执行 hdfs na ...