当前位置:网站首页>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); */
边栏推荐
- Starting from 1.5, build a micro Service Framework - call chain tracking traceid
- Paging of a scratch (page turning processing)
- Some features of ECMAScript
- Modify the ssh server access port number
- 1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
- Hundreds of lines of code to implement a JSON parser
- ORA-00030
- Overview of Zhuhai purification laboratory construction details
- Curlpost PHP
- Kotlin core programming - algebraic data types and pattern matching (3)
猜你喜欢
ADS-NPU芯片架构设计的五大挑战
基于DVWA的文件上传漏洞测试
Vulhub vulnerability recurrence 75_ XStream
Dedecms plug-in free SEO plug-in summary
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
Introduction to robotics I. spatial transformation (1) posture, transformation
95后CV工程师晒出工资单,狠补了这个,真香...
可恢复保险丝特性测试
Ubantu check cudnn and CUDA versions
Hcip---ipv6 experiment
随机推荐
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
vSphere实现虚拟机迁移
File upload vulnerability test based on DVWA
可恢复保险丝特性测试
看抖音直播Beyond演唱会有感
Convert binary search tree into cumulative tree (reverse middle order traversal)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Leetcode daily question solution: 1189 Maximum number of "balloons"
View class diagram in idea
Logstash clear sincedb_ Path upload records and retransmit log data
DD's command
程序员成长第九篇:真实项目中的注意事项
Distributed base theory
朝招金安全吗 会不会亏损本金
面试必刷算法TOP101之回溯篇 TOP34
A preliminary study of geojson
Live video source code, realize local storage of search history
FFT 学习笔记(自认为详细)
The detailed page returns to the list and retains the original position of the scroll bar
Daily practice - February 13, 2022