当前位置:网站首页>Leetcode 208. Implement trie (prefix tree)
Leetcode 208. Implement trie (prefix tree)
2022-07-06 01:16:00 【May Hacker】
subject
https://leetcode.cn/problems/implement-trie-prefix-tree/
Java AC
class Node{
char c;
Node[] next;
boolean finished;
}
class Trie {
public Node root;
public Trie() {
root = new Node();
}
public void insert(String word) {
Node currentNode = root;
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
int index = (int)(c-'a');
// System.out.println(0);
if(currentNode.next==null){
currentNode.next = new Node[26];
}
if(currentNode.next[index]==null){
Node t = new Node();
currentNode.next[index] = t;
}
currentNode = currentNode.next[index];
currentNode.c = c;
}
currentNode.finished = true;
}
public boolean search(String word) {
Node currentNode = root;
for(int i=0;i<word.length();i++){
char c = word.charAt(i);
int index = (int)(c-'a');
if(currentNode.next==null || currentNode.next[index]==null){
return false;
}
currentNode = currentNode.next[index];
if(i==word.length()-1){
return currentNode.finished;
}
}
return false;
}
public boolean startsWith(String prefix) {
Node currentNode = root;
for(int i=0;i<prefix.length();i++){
char c = prefix.charAt(i);
int index = (int)(c-'a');
if(currentNode.next==null ||currentNode.next[index]==null){
return false;
}
currentNode = currentNode.next[index];
if(i==prefix.length()-1){
return true;
}
}
return true;
}
}
/** * 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); */
边栏推荐
- Why can't mathematics give machine consciousness
- 程序员成长第九篇:真实项目中的注意事项
- Pbootcms plug-in automatically collects fake original free plug-ins
- Installation and use of esxi
- Use of crawler manual 02 requests
- Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
- DOM introduction
- Recommended areas - ways to explore users' future interests
- MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
- Tcpdump: monitor network traffic
猜你喜欢
Installation and use of esxi
MySQL learning notes 2
File upload vulnerability test based on DVWA
电气数据|IEEE118(含风能太阳能)
普通人下场全球贸易,新一轮结构性机会浮出水面
Loop structure of program (for loop)
Introduction to robotics I. spatial transformation (1) posture, transformation
Study diary: February 13, 2022
程序员搞开源,读什么书最合适?
VMware Tools安装报错:无法自动安装VSock驱动程序
随机推荐
IP storage and query in MySQL
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
Paging of a scratch (page turning processing)
037 PHP login, registration, message, personal Center Design
JVM_ 15_ Concepts related to garbage collection
VMware Tools安装报错:无法自动安装VSock驱动程序
How to get the PHP version- How to get the PHP Version?
3D model format summary
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
Obstacle detection
golang mqtt/stomp/nats/amqp
95后CV工程师晒出工资单,狠补了这个,真香...
ORA-00030
Cve-2017-11882 reappearance
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
DOM introduction
朝招金安全吗 会不会亏损本金
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
Introduction to robotics I. spatial transformation (1) posture, transformation
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号