当前位置:网站首页>Not for 58 days. Implement prefix tree
Not for 58 days. Implement prefix tree
2022-07-29 04:15:00 【A little Ming】

class Trie {
private Trie[] children;
private boolean isEnd;
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;
}
private 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) {
return null;
}
node = node.children[index];
}
return node;
}
}边栏推荐
- 基于STM32和阿里云的环境检测系统设计
- Why are there so many unknowns when opengauss starts?
- How to set the SQL execution timeout for flick SQL
- flink-sql 如何设置 sql执行超时时间
- C语言:结构体简单语法总结
- How to solve the problem of store ranking?
- %s. %c, character constant, string constant, const char*, pointer array, string array summary
- 大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
- 15.federation
- 不会就坚持63天吧 最大的异或
猜你喜欢

不会就坚持60天吧 神奇的字典

伏英娜:元宇宙就是新一代互联网!
![[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation](/img/4b/150689d5e4809ae66a4297915ecd0c.png)
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation

MySQL gets the maximum value record by field grouping

MPU6050

Machine vision series 3:vs2019 opencv environment configuration

为什么opengauss启动的时候这么多的unknown?

Const char* and char*, string constants

不会就坚持59天吧 替换单词

Common components of solder pad (2021.4.6)
随机推荐
“蔚来杯“2022牛客暑期多校训练营2 H
C declaration and initialization and assignment
C language - character array - string array - '\0' -sizeof-strlen() -printf()
通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
Safari's compatibility with Z-index
Mmdetection preliminary use
小程序:区域滚动、下拉刷新、上拉加载更多
How to set the SQL execution timeout for flick SQL
A little understanding of pointer, secondary pointer, wild pointer, pointer as function return value
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
Cad2020 introductory learning (2021.4.13)
Design of environment detection system based on STM32 and Alibaba cloud
开课!看smardaten如何分解复杂业务场景
优炫数据库有办法查到主集群每天传给备集群的日志量吗?
为什么opengauss启动的时候这么多的unknown?
Don't the JDBC SQL connector of the big guys Flink now support all databases, such as vertica?
"Weilai Cup" 2022 Niuke summer multi school training camp 2H
Change the value of the argument by address through malloc and pointer
店铺排名问题,如何解决?