当前位置:网站首页>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); */
边栏推荐
- MCU通过UART实现OTA在线升级流程
- Unity | 实现面部驱动的两种方式
- 孤勇者
- CTF daily question day44 rot
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- MYSQL---查询成绩为前5名的学生
- C language programming (Chapter 6 functions)
- Cglib dynamic agent -- example / principle
- Study diary: February 13, 2022
猜你喜欢
Cf:h. maximum and [bit operation practice + K operations + maximum and]
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
Cf:c. the third problem
I'm interested in watching Tiktok live beyond concert
Vulhub vulnerability recurrence 74_ Wordpress
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
IP storage and query in MySQL
JVM_ 15_ Concepts related to garbage collection
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
随机推荐
基于DVWA的文件上传漏洞测试
Logstash clear sincedb_ Path upload records and retransmit log data
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
SSH login is stuck and disconnected
ADS-NPU芯片架构设计的五大挑战
现货白银的一般操作方法
Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
Arduino hexapod robot
synchronized 和 ReentrantLock
[day 30] given an integer n, find the sum of its factors
WordPress collection plug-in automatically collects fake original free plug-ins
Novice entry depth learning | 3-6: optimizer optimizers
Unity | 实现面部驱动的两种方式
Construction plan of Zhuhai food physical and chemical testing laboratory
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
Promise
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
可恢复保险丝特性测试
Dede collection plug-in free collection release push plug-in
Why can't mathematics give machine consciousness