当前位置:网站首页>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); */
边栏推荐
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- Exciting, 2022 open atom global open source summit registration is hot
- Vulhub vulnerability recurrence 75_ XStream
- Idea sets the default line break for global newly created files
- servlet(1)
- 程序员成长第九篇:真实项目中的注意事项
- A preliminary study of geojson
- Programmer growth Chapter 9: precautions in real projects
- 95后CV工程师晒出工资单,狠补了这个,真香...
- VMware Tools安装报错:无法自动安装VSock驱动程序
猜你喜欢

Ubantu check cudnn and CUDA versions

Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

Introduction to robotics I. spatial transformation (1) posture, transformation

Mathematical modeling learning from scratch (2): Tools

毕设-基于SSM高校学生社团管理系统

KDD 2022 | EEG AI helps diagnose epilepsy

可恢复保险丝特性测试

Xunrui CMS plug-in automatically collects fake original free plug-ins

Beginner redis
随机推荐
Daily practice - February 13, 2022
yii中console方法调用,yii console定时任务
How to extract MP3 audio from MP4 video files?
Leetcode daily question solution: 1189 Maximum number of "balloons"
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
SCM Chinese data distribution
Differences between standard library functions and operators
IP storage and query in MySQL
Some features of ECMAScript
Overview of Zhuhai purification laboratory construction details
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
Three methods of script about login and cookies
A preliminary study of geojson
黄金价格走势k线图如何看?
Live broadcast system code, custom soft keyboard style: three kinds of switching: letters, numbers and punctuation
Cf:h. maximum and [bit operation practice + K operations + maximum and]
golang mqtt/stomp/nats/amqp
[day 30] given an integer n, find the sum of its factors
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem