当前位置:网站首页>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); */
边栏推荐
- Leetcode 剑指 Offer 59 - II. 队列的最大值
- Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
- After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
- In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
- golang mqtt/stomp/nats/amqp
- Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
- Dynamic programming -- linear DP
- Construction plan of Zhuhai food physical and chemical testing laboratory
- 在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
- What is the most suitable book for programmers to engage in open source?
猜你喜欢
Recoverable fuse characteristic test
Some features of ECMAScript
yii中console方法调用,yii console定时任务
Loop structure of program (for loop)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Cf:c. the third problem
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
JVM_ 15_ Concepts related to garbage collection
ThreeDPoseTracker项目解析
黄金价格走势k线图如何看?
随机推荐
Hcip---ipv6 experiment
Differences between standard library functions and operators
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
False breakthroughs in the trend of London Silver
伦敦银走势中的假突破
Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
WordPress collection plug-in automatically collects fake original free plug-ins
Unity | 实现面部驱动的两种方式
Zhuhai's waste gas treatment scheme was exposed
golang mqtt/stomp/nats/amqp
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
GNSS terminology
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
Dede collection plug-in free collection release push plug-in
Cf:c. the third problem
What is the most suitable book for programmers to engage in open source?
Novice entry depth learning | 3-6: optimizer optimizers
WGet: command line download tool
Kotlin core programming - algebraic data types and pattern matching (3)
Loop structure of program (for loop)