当前位置:网站首页>Trie (dictionary tree)
Trie (dictionary tree)
2022-04-21 20:53:00 【lideng4523】
Each node store can be mapped with a hash Map<Character, Trie> map,
You can also use arrays Trie[] children
class Trie {
private Map<Character, Trie> map;
private boolean endFlag;
public Trie() {
map = new HashMap<>();
endFlag = false;
}
public void insert(String word) {
Trie node = this;
for(int i = 0; i < word.length(); i++) {
if(!node.map.containsKey(word.charAt(i))) {
Trie tmp = new Trie();
node.map.put(word.charAt(i), tmp);
}
node = node.map.get(word.charAt(i));
}
node.endFlag = true;
}
public boolean search(String word) {
Trie node = this;
for(int i = 0; i < word.length(); i++) {
if(node.map.containsKey(word.charAt(i))) {
node = node.map.get(word.charAt(i));
} else {
return false;
}
}
return node.endFlag == true ? true : false;
}
public boolean startsWith(String prefix) {
Trie node = this;
for(int i = 0; i < prefix.length(); i++) {
if(node.map.containsKey(prefix.charAt(i))) {
node = node.map.get(prefix.charAt(i));
} else {
return false;
}
}
return true;
}
}
/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */
版权声明
本文为[lideng4523]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212048459759.html
边栏推荐
- AttributeError: ‘list‘ object has no attribute ‘endswich‘
- In depth analysis of TCP three handshakes, the interviewer applauded
- 如何正确有效的进行滑环的安装
- Tracup|使用项目管理软件帮助战胜拖延症
- 深度剖析TCP三次握手,面试官拍案叫绝
- Andorid - - Pourquoi utiliser une transaction, qu'est - ce qu'une transaction commit et ROLLBACK?
- RTMP(3):Protocol Control Message
- String. Length () and string getBytes(). Length difference
- 2022年电工(初级)考试模拟100题及模拟考试
- shell:变量
猜你喜欢
随机推荐
track和trigger
5、Qt使用MySQL
Tracup|使用项目管理独一无二的六大好处
复线性空间与复结构
Live555学习
神经网络 || 注意力机制的Pytorch代码实现
Andorid - - Pourquoi utiliser une transaction, qu'est - ce qu'une transaction commit et ROLLBACK?
Andorid --- 为什么要使用事务,什么叫做事务的提交和回滚?
【合泰ht32与stm32进行串口通信点灯】
分布式秒杀系统构建
Tips for using win10 close user tips before software installation
Wiki.js 配置 LDAP 认证
2、Failed to connect to MySQL Server 8.0.28 after 10 attempts
为何PostgreSQL即将超越SQL Server?
oracle数据导入记录笔记
UAV assembly and debugging tutorial
After five years of outsourcing, I'm almost a loser
What are the material requirements of the electric slip ring
Go语言自学系列 | golang指针
Andorid --- 為什麼要使用事務,什麼叫做事務的提交和回滾?



![[network security] stapler1 of red team penetration project (Part 2)](/img/39/7d5594da6e7e89e510040b66eef383.png)
![[Hetai ht32 communicates with STM32 through serial port and lights up]](/img/77/750edf9608d8661856afbd43449690.png)




