当前位置:网站首页>LeetCode.208. 实现 Trie (前缀树)____字典树
LeetCode.208. 实现 Trie (前缀树)____字典树
2022-07-20 02:09:00 【向光.】
208. 实现 Trie (前缀树)
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
- Trie() 初始化前缀树对象。
- void insert(String word) 向前缀树中插入字符串 word 。
- boolean search(String word) 如果字符串 word 在前缀树中,返回
true(即,在检索之前已经插入);否则,返回 false - boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回
true ;否则,返回 false
示例:
输入
[“Trie”, “insert”, “search”, “search”, “startsWith”, “insert”, “search”]
[[], [“apple”], [“apple”], [“app”], [“app”], [“app”], [“app”]]
输出
[null, null, true, false, true, null, true]
解释
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
提示:
- 1 <= word.length, prefix.length <= 2000
- word 和 prefix 仅由小写英文字母组成
- insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
Solution:
- 关于字典树,我们可以通过一个视频Trie前缀树和一个博客Java实现Trie前缀树进行了解。
Code:
/** * 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); */
class Trie{
Node root;
public Trie(){
root = new Node();
}
class Node{
Node[] nodes;
boolean flag;
public Node(){
nodes = new Node[26]; // 看样子是直接开了26个坑,但是其实由于每一个坑都没有new,所以都没数据,所以其实只是假开,只是把26个指向开了,
// 后面只有真正存在某个方向的元素才会真开
flag = false;
}
public boolean contains(char key){
if(nodes[key - 'a'] != null)
return true;
return false;
}
public Node get(char key){
return nodes[key - 'a'];
}
public void put(char key){
nodes[key - 'a'] = new Node();
}
public void setFlag(){
flag = true;
}
public boolean checkFlag(){
return flag;
}
}
public void insert(String word){
Node temp = root;
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
if(temp.contains(c)){
temp = temp.get(c);
}
else{
temp.put(c);
temp = temp.get(c);
}
}
temp.flag = true;
}
public boolean search(String word){
Node temp = root;
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
if(temp.contains(c)){
temp = temp.get(c);
}
else{
return false;
}
}
return temp.flag == true ? true : false ;
}
public boolean startsWith(String prefix){
Node temp = root;
for(int i=0;i<prefix.length();i++){
char c = prefix.charAt(i);
if(temp.contains(c)){
temp = temp.get(c);
}
else{
return false;
}
}
return true;
}
}
边栏推荐
- 网络安全—综合渗透测试-CVE-2010-2883-PDF漏洞分析
- 【03】通过你的CPU主频,我们来谈谈“性能”究竟是什么?
- SQL 注入攻击风险
- acwing 867. 分解质因数
- Blurred photos, second high-definition big picture, flying propeller ppde takes you to reproduce the image restoration model cmfnet
- 中职网络安全技能大赛P100-Dcore(轻型CMS系统)SQL注入
- PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!
- 通过TCP方式点灯
- Baidu PaddlePaddle easydl helps manufacturing enterprises with intelligent transformation
- Dix minutes pour générer un effet de design intérieur de qualité film et télévision, comment mettre à niveau l'industrie de la maison traditionnelle avec Red Star McLaren Design Cloud
猜你喜欢
网络安全—综合渗透测试-CVE-2018-10933-libssh维持访问
Kubernetes kube-scheduler调度器
ViT【backbone】
How can mechanical manufacturing enterprises solve warehouse management problems with ERP system?
网络安全—综合渗透测试-CVE-2019-15107-Webmin远程代码执行
LeetCode_ 90_ Subset II
acwing 867. 分解质因数
中职网络安全—2022年国赛逆向PE Reverse解题思路
30万奖池等你来战!自然语言处理(NLP)赛事合集来啦
网络安全—综合渗透测试-CVE-2017-12629-Apache Solr远程代码执行
随机推荐
HW2021攻防演练经历碎碎念-见解
Question 128 of Li Kou: longest continuous sequence
The five safety links that are easy to be ignored are more dangerous than expected!
Xshell & putty color scheme
Iptables prevent nmap port scanning
统计代码耗时的一个不常用方法
Learn about spark project on nebulagraph
十分鐘生成影視級室內設計效果,紅星美凱龍設計雲如何昇級傳統家居行業
Harbor - image warehouse
xtrabackup实现mysql:全量备份+增量备份
Tutorial on principles and applications of database system (033) -- data integrity of MySQL (VI): not NULL constraint
数据库系统原理与应用教程(023)—— MySQL 创建数据表的各种方法总结
数据库系统原理与应用教程(022)—— MySQL 支持的数据类型总结
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
Encapsulation, inheritance, polymorphism
Network security in Secondary Vocational Schools - the thinking of reverse PE reverse problem solving in 2022 National Games
Unity:PC开发,鼠标点击物体触发物体更换材质
Baidu PaddlePaddle easydl helps manufacturing enterprises with intelligent transformation
Harbor—镜像仓库
Tutorial on principles and applications of database system (027) -- MySQL modifying data in tables (III): update