当前位置:网站首页>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;
}
}边栏推荐
- When defining an array, the size must be constant
- How to write the filter conditions of data integration and what syntax to use? SQL syntax processing bizdate can not be
- 为什么opengauss启动的时候这么多的unknown?
- C language - character array - string array - '\0' -sizeof-strlen() -printf()
- Locker 2022.1.1
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- Svg -- loading animation
- Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
- Compilation and linking
- 不会就坚持64天吧 查找插入位置
猜你喜欢

The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout

编译与链接

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

Object detection: object_ Detection API +ssd target detection model

How to solve the problem of store ranking?

Record of problems encountered in ROS learning

不会就坚持68天吧 狒狒吃香蕉

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation

MPU6050

基于STM32和阿里云的环境检测系统设计
随机推荐
Multi rotor six axis hardware selection
Jenkins 参数化构建中 各参数介绍与示例
Leftmost prefix principle of index
Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
Taobao product details interface (product details page data interface)
How to execute insert into select from job in SQL client
2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
不会就坚持66天吧 权重生成随机数
Const read only variable constant
Object detection: object_ Detection API +ssd target detection model
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
数据库SQL语句实现数据分解的函数查询
The table of antd hides the pager when there is only one page
AssertionError(“Torch not compiled with CUDA enabled“)
[common commands]
Fuzzy query of SQL
这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
Solution: module 'xlrd' has no attribute 'open_ Error reporting of workbook '
C语言:浅谈各种复杂的声明