当前位置:网站首页>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;
}
}边栏推荐
- SVG--loading动画
- Methods of using multiple deformations on an element
- Taobao product details interface (product details page data interface)
- 小程序:区域滚动、下拉刷新、上拉加载更多
- How to query the submission number of a version
- Why are there so many unknowns when opengauss starts?
- Shielding ODBC load balancing mode in gbase 8A special scenarios?
- Fu Yingna: Yuan universe is the new generation of Internet!
- Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
- What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
猜你喜欢

Machine vision Series 2: vs DLL debugging

Class starts! See how smardaten decomposes complex business scenarios

Why are there so many unknowns when opengauss starts?

Design of environment detection system based on STM32 and Alibaba cloud
![[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)](/img/fe/8c707c30c734de7bb76ea68134842c.png)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)

Beginner: array & String

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

11.备份交换机

不会就坚持69天吧 合并区间

Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
随机推荐
Svg -- loading animation
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
C语言:联合体知识点总结
不会就坚持60天吧 神奇的字典
The table of antd hides the pager when there is only one page
不会就坚持64天吧 查找插入位置
Locker 2022.1.1
MySQL gets the maximum value record by field grouping
How to query the submission number of a version
Differences and principles of bio, NiO and AIO
There is a special cryptology language called asn.1
索引的最左前缀原理
Model tuning, training model trick
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
Design of environment detection system based on STM32 and Alibaba cloud
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
The function "postgis_version" cannot be found when installing PostGIS
[kvm] install KVM
C language: getchar () and cache
Code or script to speed up the video playback of video websites