当前位置:网站首页>不会就坚持58天吧 实现前缀树
不会就坚持58天吧 实现前缀树
2022-07-29 04:13:00 【一只小小明】

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;
}
}边栏推荐
- 请问,在sql client中,执行insert into select from job时,如何单
- Compilation and linking
- [deep learning CPU (part outside) - virtual memory]
- “蔚来杯“2022牛客暑期多校训练营1 J Serval and Essay(启发式合并)
- Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
- HCIP BGP
- C语言:typedef知识点总结
- Taobao product details interface (product details page data interface)
- RMAN do not mark expired backups
- Openfeign asynchronous call problem
猜你喜欢

MySQL gets the maximum value record by field grouping

Function pointer and callback function

How to solve the problem of store ranking?

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

【深度学习CPU(番外篇)——虚拟内存】

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

力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)

STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案

Data mining -- code implementation of association analysis example (Part 2)

Svg -- loading animation
随机推荐
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
数据库SQL语句实现数据分解的函数查询
Codeforces Round #810 (Div. 2) D. Rain (线段树差分)
HCIP BGP
Data mining -- Introduction to the basis of association analysis (Part 1)
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
SVG--loading动画
Problems encountered in vscode connection SSH
%s. %c, character constant, string constant, const char*, pointer array, string array summary
LCA 板子
Communication between parent-child components and parent-child components provide and inject
First knowledge of C language (3)
C language to achieve three chess game (detailed explanation)
Nacos registry
How to query the submission number of a version
How to set the SQL execution timeout for flick SQL
JS realizes the function of one click Copy
Summary on the thought of double pointer
开课!看smardaten如何分解复杂业务场景