当前位置:网站首页>208. Implement trie (prefix tree)
208. Implement trie (prefix tree)
2022-04-22 18:15:00 【A research monk who doesn't like research】
subject :
Trie( It sounds like "try") Or say Prefix tree It's a tree data structure , Keys for efficient storage and retrieval of string datasets . This data structure has quite a number of application scenarios , For example, automatic completion and spelling check .
Please realize Trie class :
Trie() Initialize the prefix tree object .
void insert(String word) Insert a string into the forward tree word .
boolean search(String word) If the string word In the prefix tree , return true( namely , Has been inserted before retrieval ); otherwise , return false .
boolean startsWith(String prefix) If a string has been inserted before word One of the prefixes of is prefix , return true ; otherwise , return false .
Answer key :
Introduction to dictionary tree :



startsWith and search Function has only the final judgment isEnd The difference between , So with searchPrefix The function implements the operation in front of the two functions .
class Trie {
private Trie[] children;
private boolean isEnd; // Whether the node is the end of a string
public Trie() {
children = new Trie[26];
isEnd = false;
}
public void insert(String word) {
Trie node = this;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
int index = ch - 'a';
if (node.children[index] == null) {
node.children[index] = new Trie();
}
node = node.children[index];
}
node.isEnd = true;
}
public boolean search(String word) {
Trie node = searchPrefix(word);
return node != null && node.isEnd;
}
public boolean startsWith(String prefix) {
return searchPrefix(prefix) != null;
}
//startsWith and search Function has only the final judgment isEnd The difference between , So with searchPrefix The function implements the operation in front of the two functions
public Trie searchPrefix(String prefix) {
Trie node = this;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
int index = ch - 'a';
if (node.children[index] == null) { // There is no quote on the corresponding character , Is the current node It's empty
return null;
}
node = node.children[index];
}
return node; // Returns the last character of a word node
}
}
/**
* 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);
*/
Reference resources :
版权声明
本文为[A research monk who doesn't like research]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221811013861.html
边栏推荐
- 在 Kubernetes 集群中部署现代应用的通用模式
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验!
- 【接口测试基础】第十一篇 | 详解Postman关联接口及批量执行用例集
- Unable to translate SQLException with Error code ‘0‘, will now try the fallback translator
- [Lane] ultra fast lane detection (2) custom model test
- Codeforces Round #784 (Div. 4) AK题解
- Flink之转换算子 (Transformation)
- Zhihu hot discussion: Zhejiang University has studied Bo for eight years and now makes money by delivering takeout
- Why has Sony always dominated the smartphone sensor market
- 使用docker创建mysql主从备份时遇到的一些问题
猜你喜欢

【思考与进步】:关于自己的遗憾

【C语言进阶篇】一篇文章带你认清结构、枚举和联合

leetcode-470. 用 Rand7() 实现 Rand10()

MySQL - index

Notes on soft test high items | contents of detailed feasibility study
![B树[概念]](/img/de/e52df3061a1615291de9b29c7b5c5c.png)
B树[概念]

【bat】查看文件md5

18730 coloring problem (two ways of writing fast power)

Spacy first routine (automatic annotation of Chinese text)

Use of ES6 generator function
随机推荐
Design the test paper storage scheme of ten million students management system
Learning documents.
广东水泥数据概况
华为路由器通过MPLS 虚拟私有网实现总部与分公司连接
[2021] Tencent autumn recruitment technology post programming arrangement supermarket
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
目前国产电脑硬件的现状是怎样的?
Pytoch Note58 CNN可视化
多次调用 BAPI 之后,最后一次性 COMMIT WORK,会有什么问题吗?
Usage of SAP ABAP for all entries
持续有效的风险指标:动荡指数
Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
leetcode - 234. 回文链表
【思考与进步】:关于自己的遗憾
工具类XMLUtil(解析返回soap和xml报文,获取目标节点值)
第119章 SQL函数 RIGHT
In 2022, it is said on the Internet that Apple's upcoming new models iPhone 14 pro and iPhone 14 Pro Max will be a new screen shape, not a banged screen. Do you expect the iPhone 14 with a new screen
Notes on soft test high items | contents of feasibility study
『程序员延寿指南』:跟着码农干,多活20年!
SAP ABAP FOR ALL ENTRIES 的用法