当前位置:网站首页>[brother hero July training] day 23: dictionary tree
[brother hero July training] day 23: dictionary tree
2022-07-24 02:43:00 【If I were Wen Shuai】
List of articles
- One 、1032. Character stream
- leetcode1032 java
- Ideas
- Construct a dictionary tree ( Whether the leaf node \next)
- initialization , Here, the incoming characters are written into the dictionary tree in reverse order , Such as "cd", "f", "kl", Write as dcflk
- To find the character , Write the first node every time , Such as query a,b,c,d by lookup dcba , from d Start looking for , And if the leaf node , return true, That is to dc Medium c It's about , Meet the conditions , return true
- summary
One 、1032. Character stream
Design an algorithm : Receive a character stream , And check whether the suffix of these characters is a string array words A string in .
for example ,words = [“abc”, “xyz”] And the character stream is added one by one 4 Characters ‘a’、‘x’、‘y’ and ‘z’ , The algorithm you designed should be able to detect “axyz” The suffix “xyz” And words String in “xyz” matching .
According to the following requirements StreamChecker class :
StreamChecker(String[] words) : Constructors , Use a string array words Initialize data structure .
boolean query(char letter): Receive a new character from the character stream , If any non empty suffix in the character stream can match words A string in , return true ; otherwise , return false.
leetcode1032 java
Ideas
Construct a dictionary tree ( Whether the leaf node \next)
initialization , Here, the incoming characters are written into the dictionary tree in reverse order , Such as "cd", “f”, “kl”, Write as dcflk
To find the character , Write the first node every time , Such as query a,b,c,d by lookup dcba , from d Start looking for , And if the leaf node , return true, That is to dc Medium c It's about , Meet the conditions , return true
class StreamChecker {
public class TrieNode{
boolean isleaf;
TrieNode[] next;
public TrieNode(){
next = new TrieNode[26];
}
}
public void insert(String word){
if(word==null||word.length()==0) return;
TrieNode node = root;
for(int i =0;i<word.length();i++){
char c = word.charAt(i);
TrieNode temp = node.next[c-'a'];
if(temp==null){
temp = new TrieNode();
node.next[c-'a']=temp;
}
node = node.next[c-'a'];
}
node.isleaf = true;
}
int maxlength = 0;
TrieNode root;
StringBuilder stream = new StringBuilder();
public StreamChecker(String[] words) {
root = new TrieNode();
for(String word:words){
maxlength = Math.max(maxlength,word.length());
insert(new StringBuilder(word).reverse().toString());
}
}
public boolean query(char letter) {
stream.insert(0,letter);
if(stream.length()>maxlength) stream.deleteCharAt(stream.length()-1);
TrieNode temp = root;
for(int i = 0;i<stream.length();i++){
if(temp.next[stream.charAt(i)-'a']==null) return false;
temp = temp.next[stream.charAt(i)-'a'];
if(temp.isleaf) return true;
}
return temp.isleaf;
}
}
summary
边栏推荐
- [FPGA tutorial case 39] communication case 9 - interleaving deinterleaving data transmission based on FPGA
- 【补题日记】[2022牛客暑期多校1]I-Chiitoitsu
- Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
- Cloud native explanation [expansion]
- Rylstim Screen Recorder
- 【补题日记】[2022杭电暑期多校1]B-Dragon slayer
- Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特
- 攻防世界WEB练习区(webshell、command_execution、simple_js)
- JS when transferring parameters, the incoming string has data; No data when number is passed in; 2[0] is right! Number type data can be subscripted
- [knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
猜你喜欢

Causal learning open source project: from prediction to decision!

攻防世界WEB練習區(view_source、get_post、robots)

TP5 framework link promotion project
![La chaîne entrante a des données lors de la transmission des paramètres JS; Aucune donnée n'a été transmise au numéro; 2 [0] Oui! Les données de type numéro peuvent être indexées](/img/4e/3d0c25d9579b6d5c00473048dbbd83.png)
La chaîne entrante a des données lors de la transmission des paramètres JS; Aucune donnée n'a été transmise au numéro; 2 [0] Oui! Les données de type numéro peuvent être indexées

Reading notes: self cultivation of programmers - Chapter 3

508. The subtree element with the most occurrences and the pure C implementation of hash table method
![js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标](/img/4e/3d0c25d9579b6d5c00473048dbbd83.png)
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标

Pbootcms template calls the tag ordinal number from 2 or automatic number

Opensmile introduction and problems encountered during installation

ssm的求职招聘系统兼职应聘求职
随机推荐
通用机环境下安全版单机数据库使用非root用户管理的解决方案
Essential skills for programmers -- breakpoint debugging (idea version)
Recorded on July 21, 2022
Audio processing based on time-frequency diagram matlab
中城院真的在帮助供应商解决问题吗?
Recommendation system topic | recommendation system architecture and single domain cross domain recall model
About offline use of SAP Fiori application
[diary of supplementary questions] [2022 Hangdian summer school 1] c-backpack
Unity TimeLine使用教程
Analyze the overall planning of steam and maker education classroom
无需编码,自动实现“异步 Request-Reply”模式
Share an API Gateway project based on ABP and yarp
Custom log annotation, request fetching
SIGIR‘22 推荐系统论文之多样性篇
Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
【FPGA教程案例38】通信案例8——基于FPGA的串并-并串数据传输
Pbootcms template calls the tag ordinal number from 2 or automatic number
"Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture
Force open web page
Maximize, minimize, restore, close and move the WinForm form form of C #