Leetcode 208.实现前缀树
实现前缀树
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:
- 你可以假设所有的输入都是由小写字母 a-z 构成的。
- 保证所有输入均为非空字符串。
dclass TrieNode{
char c;
boolean leaf;
HashMap<Character,TrieNode> children=new HashMap<Character,TrieNode>();
public TrieNode(char c){
this.c=c;
}
public TrieNode(){};
} public class Trie{
private TrieNode root;
public Trie(){
root=new TrieNode();
} public void insert(String word){
Map<Character,TrieNode> children=root.children;
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
TrieNode t;
if(children.containsKey(c)){
t=children.get(c);
}else{
t=new TrieNode(c);
children.put(c,t);
}
children=t.children;
if(i==word.length()-1)
t.leaf=true;
}
} public boolean search(String word){
TrieNode t=searchNode(word);
return t!=null && t.leaf;
} private TrieNode searchNode(String word){
Map<Character,TrieNode> children=root.children;
TrieNode t=null;
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
if(!children.containsKey(c)) return null;
t=children.get(c);
children=t.children;
}
return t;
} public boolean startsWith(String prefix){
return searchNode(prefix)!=null;
}
}
Leetcode 208.实现前缀树的更多相关文章
- LeetCode 实现 Trie (前缀树)
题目链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree/ 题目大意: 略. 分析: 字典树模板. 代码如下: class Tr ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Java实现 LeetCode 208 实现 Trie (前缀树)
208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...
- [leetcode] 208. 实现 Trie (前缀树)(Java)
208. 实现 Trie (前缀树) 实现Trie树,网上教程一大堆,没啥可说的 public class Trie { private class Node { private int dumpli ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- [LeetCode] 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)空间复杂度:字典树远远小于哈 ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
随机推荐
- 贪心 CodeForces 124B Permutations
题目传送门 /* 贪心:全排列函数使用,更新最值 */ #include <cstdio> #include <algorithm> #include <cstring& ...
- linux下安装xampp
Choose your flavor for your linux OS, the 32-bit or 64-bit version. Change the permissions to the in ...
- 在SQLServer 2005附加SQLServer 2008数据库异常处理
远程服务器软件系统不算新,数据库是SQL Server 2005.本地开发基本是用新的软件系统.数据库采用SQL Server 2008. 这样在用远程服务器SQL 2005选择附加SQL 2008的 ...
- linux下mysql开启可访问
修改mysql配置连接信息 将bind-address注释 vim /etc/my.cnf 修改mysql用户授权 mysql>GRANT ALL PRIVILEGES ON *.* TO ' ...
- iOS循环引用
iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...
- 无聊的我写了一个代码 。。。P1605 迷宫
搜索水题 哎 直接不行了 . #include <ctype.h> #include <cstdio> void read(int &x) { x=;char ch=g ...
- Servlet相关的几种中文乱码问题
Servlet相关的几种中文乱码问题浏览器调用jsp,html等页面中文显示乱码使得文件本身以utf-8字符集编辑保存 让浏览器浏览器以utf-8字符集解析 在浏览器中右键选择编码格式为utf-8: ...
- 修改phpadmin中的默认超时时间
登录后1440秒未活动后总是自动退出,一天还要登录多次,终于有时间来解决这个问题了,感觉是session超时,结果在网上search了下,找到解决办法啦,哈哈哈,在此做个笔记: phpmyadmin在 ...
- WPF小记 -- 使用Path自己画图标,点击命中(焦点)丢失问题
在Template中,Path外面的Grid需添加Background属性值.否则点击范围会受限制,例如:Click,在RadioButton的Height和With范围内点击,命中率<1. & ...
- windows sdk编程为应用程序添加图标
#include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...