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 ...
随机推荐
- 洛谷 P2312 解方程
题目 首先,可以确定的是这题的做法就是暴力枚举x,然后去计算方程左边与右边是否相等. 但是noip的D2T3怎么会真的这么简单呢?卡常卡的真是熟练 你需要一些优化方法. 首先可以用秦九韶公式优化一下方 ...
- 题解报告:hdu 1114 Piggy-Bank(完全背包恰好装满)
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- Retinex系列之Frankle-McCann Retinex 分类: Matlab 图像处理 2014-12-01 21:52 538人阅读 评论(2) 收藏
一.Frankle-McCann Retinex Frankle-McCann算法选择一条螺旋结构的路径用于像素间的比较.如下图,算法沿着螺旋路径选取用于比较 像素点,这种路径选择包含了整个图像的全局 ...
- Eclipse快捷键集合
***eclipse查看哪个方法被调用:选中,右键选择Open call Hierarchy 选择要查看的方法: ctrl + alt + h 查看一个类被那些类继承或者实现: F ...
- 12 DOM操作应用
1.创建子元素oLi=document.creatElement('li') 2.将元素附给父级元素oUl.appendChild(oLi) 3.将元素插入到父级元素里的第一位子元素之前oUl.ins ...
- 461在全志r16平台tinav3.0系统下使用地磁计QMC5883L
461在全志r16平台tinav3.0系统下使用地磁计QMC5883L 2018/9/7 14:08 版本:V1.0 开发板:SC3817R SDK:tina v3.0 (基本确认全志tina v3. ...
- 在 Windows Server 上搭建 *** 服务端(转载加亲测)
转载自:https://diveng.io/build-shadowsocks-server-on-windows-server.html 下面的教程建议大家使用第一种方法安装,说是比较简单.我则使用 ...
- c# winform如何屏蔽键盘上下左右键
重写事件: protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Up || keyData == ...
- IE浏览器样式表限制
原文链接:http://caibaojian.com/ie-stylesheet.html 在开发头条上发现的IE浏览器样式限制,算是一个IE浏览器的一个bug吧.主要有4点限制:· IE9及以下单个 ...
- ztree 展开一级节点 | ztree只显示到二级目录
// 默认展开一级节点var nodes = tree.getNodesByParam("level", 0);for (var i = 0; i < nodes.lengt ...