当前位置:网站首页>Leetcode 208. 实现 Trie (前缀树)
Leetcode 208. 实现 Trie (前缀树)
2022-07-06 01:10:00 【May Hacker】
题目
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); */
边栏推荐
- 从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
- [groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
- cf:C. The Third Problem【关于排列这件事】
- logstash清除sincedb_path上传记录,重传日志数据
- The detailed page returns to the list and retains the original position of the scroll bar
- 《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
- The population logic of the request to read product data on the sap Spartacus home page
- Recursive method to realize the insertion operation in binary search tree
- FFT 学习笔记(自认为详细)
- For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
猜你喜欢
Keepalive component cache does not take effect
KDD 2022 | EEG AI helps diagnose epilepsy
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
View class diagram in idea
Recursive method to realize the insertion operation in binary search tree
Exciting, 2022 open atom global open source summit registration is hot
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
基于DVWA的文件上传漏洞测试
看抖音直播Beyond演唱会有感
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
随机推荐
基於DVWA的文件上傳漏洞測試
IP storage and query in MySQL
Differences between standard library functions and operators
直播系统代码,自定义软键盘样式:字母、数字、标点三种切换
Recursive method to realize the insertion operation in binary search tree
Promise
程序员成长第九篇:真实项目中的注意事项
Dedecms plug-in free SEO plug-in summary
Some features of ECMAScript
Mysql--- query the top 5 students
Vulhub vulnerability recurrence 75_ XStream
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
Paging of a scratch (page turning processing)
Finding the nearest common ancestor of binary tree by recursion
BiShe - College Student Association Management System Based on SSM
ThreeDPoseTracker项目解析
FFT learning notes (I think it is detailed)
Interview must brush algorithm top101 backtracking article top34