当前位置:网站首页>208. 实现 Trie (前缀树)
208. 实现 Trie (前缀树)
2022-07-30 10:30:00 【安吉_lh1029】
1、题目描述

2、题目实现
根据题目进行分析,这个可以使用哈希表进行记录,insert就是写入哈希表的过程,search就是查询哈希表的过程,如果哈希表中查询不到,就返回false, 哈希表中查询到,就返回true. 思路还是很清晰的。
使用【哈希表】进行记录模式,实现如下:
class Trie {
Set<String> map;
public Trie() {
map = new HashSet<String>();
}
public void insert(String word) {
map.add(word);
}
public boolean search(String word) {
return map.contains(word);
}
//boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
public boolean startsWith(String prefix) {
int preLength = prefix.length();
for(String s : map){
int i = 0;
for(; i < preLength ; i++){
if( i < s.length() &&
prefix.charAt(i) != s.charAt(i))
break;
}
if(i<= s.length()
&& i == preLength){
return true;
}
}
return false;
}
}
/**
* 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);
*/备注:这种实现方法的复杂度有点高,进行编译的时候, 耗时过多。可以进行进一步的优化。

边栏推荐
- 软考 系统架构设计师 简明教程 | 系统运行与软件维护
- OC-ARC (Automatic Reference Counting) automatic reference counting
- mysql与redis 区别
- Re17: Read the paper Challenges for Information Extraction from Dialogue in Criminal Law
- idea2021+Activiti [the most complete note one (basic use)]
- Basemap and Seaborn
- [AGC] Growth Service 2 - In-App Message Example
- Mysterious APT Attack
- Some commands of kubernetes
- log4js入门
猜你喜欢
随机推荐
Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
梅科尔工作室-看鸿蒙设备开发实战笔记五——驱动子系统开发
Detailed explanation of JVM memory layout, class loading mechanism and garbage collection mechanism
线上靶机prompt.ml
在机器人行业的专业人士眼里,机器人行业目前的情况如何?
In 2022, the top will be accepted cca shut the list
Log4j有哪几种日志级别呢?
paging
数据库事务,JDBC操作和数据类型
软考 系统架构设计师 简明教程 | 案例分析 | 需求分析
Multithreading--the usage of threads and thread pools
[AGC] Growth Service 2 - In-App Message Example
Security思想项目总结
Soft Exam System Architect Concise Tutorial | Case Analysis | Requirement Analysis
Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
async.js入门
AB测试 总结归纳
Meikle Studio - see the actual combat notes of Hongmeng equipment development five - drive subsystem development
Js array operating mobile for encapsulation
STM32CubeMX configuration to generate FreeRTOS project







![【 HMS core 】 【 Analytics Kit] [FAQ] how to solve the payment amount in huawei pay analysis shows zero problem?](/img/f3/b9256fc04d1c9e15c74d2fc14db0fb.png)
