当前位置:网站首页>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); */
边栏推荐
- Building core knowledge points
- Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
- [groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
- Is chaozhaojin safe? Will it lose its principal
- Modify the ssh server access port number
- Introduction to robotics I. spatial transformation (1) posture, transformation
- WordPress collection plug-in automatically collects fake original free plug-ins
- Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
- synchronized 和 ReentrantLock
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
猜你喜欢
Cannot resolve symbol error
可恢复保险丝特性测试
Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
Exciting, 2022 open atom global open source summit registration is hot
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
How to extract MP3 audio from MP4 video files?
毕设-基于SSM高校学生社团管理系统
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
Convert binary search tree into cumulative tree (reverse middle order traversal)
关于softmax函数的见解
随机推荐
Curlpost PHP
Hundreds of lines of code to implement a JSON parser
cf:C. The Third Problem【关于排列这件事】
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
朝招金安全吗 会不会亏损本金
ADS-NPU芯片架构设计的五大挑战
I'm interested in watching Tiktok live beyond concert
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
现货白银的一般操作方法
Opinions on softmax function
Arduino hexapod robot
Mysql--- query the top 5 students
Finding the nearest common ancestor of binary tree by recursion
小程序容器可以发挥的价值
golang mqtt/stomp/nats/amqp
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
Cglib dynamic agent -- example / principle
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
VMware Tools安装报错:无法自动安装VSock驱动程序
JVM_ 15_ Concepts related to garbage collection