当前位置:网站首页>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); */
边栏推荐
- Unity | 实现面部驱动的两种方式
- 有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
- ThreeDPoseTracker项目解析
- Paging of a scratch (page turning processing)
- The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
- RAID disk redundancy queue
- Dedecms plug-in free SEO plug-in summary
- Leetcode 剑指 Offer 59 - II. 队列的最大值
- ADS-NPU芯片架构设计的五大挑战
- servlet(1)
猜你喜欢

cf:C. The Third Problem【关于排列这件事】

VSphere implements virtual machine migration

Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates

Loop structure of program (for loop)

伦敦银走势中的假突破

Fibonacci number

基於DVWA的文件上傳漏洞測試

MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练

Condition and AQS principle

Some features of ECMAScript
随机推荐
Live video source code, realize local storage of search history
Five challenges of ads-npu chip architecture design
WordPress collection plug-in automatically collects fake original free plug-ins
Cannot resolve symbol error
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Zhuhai laboratory ventilation system construction and installation instructions
可恢复保险丝特性测试
Recoverable fuse characteristic test
Leetcode study - day 35
A preliminary study of geojson
Cf:c. the third problem
Redis' cache penetration, cache breakdown, cache avalanche
Four commonly used techniques for anti aliasing
现货白银的一般操作方法
ORA-00030
Leetcode 剑指 Offer 59 - II. 队列的最大值
037 PHP login, registration, message, personal Center Design
Mysql--- query the top 5 students
View class diagram in idea