LeetCode 208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)(C++/Java)
题目:
Implement a trie with insert, search, and startsWith methods.
Example:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns true
Note:
- You may assume that all inputs are consist of lowercase letters
a-z. - All inputs are guaranteed to be non-empty strings.
分析:
实现一个前缀树,包含insert, search, and startsWith三个方法,分别对应插入,搜索单词和搜索前缀三个方法。
前缀树在这里就不多说了,网上的介绍有很多。
下面说一下具体的实现。
因为只有a-z共26个英文字母,所有开辟一个26大小的数组用来保存节点,isEnd用来表示该节点是否为单词终止节点,例如apple,那么在e节点时应该将isEnd置为true,以便搜索方法查询单词。
Trie* node[26] = {nullptr};
bool isEnd = false;
insert插入一个单词,遍历单词的每一个字符,从根节点开始查询对应位置的数组是否为空,如果为空就在对应位置新建节点,然后继续,在最后一个字符对应的位置isEnd记为true
void insert(string word) {
Trie* p = this;
for(const char ch:word){
if(p->node[ch-'a'] == nullptr)
p->node[ch-'a'] = new Trie();
p = p->node[ch-'a'];
}
p->isEnd = true;
}
search, and startsWith可以通过一个辅助函数来实现,遍历单词的字符,如果对应位置为null则表示前缀树中没有这个字符,返回null即可,否自返回查询到的最后一个结点。通过结点来判断单词是否存在,如果isEnd为true则search返回true。
Trie* mySearch(string word){
Trie *p = this;
for (auto c : word){
if (p->node[c - 'a'] == nullptr) return nullptr;
p = p->node[c - 'a'];
}
return p;
}
程序:
C++
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
//root = new Trie();
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* p = this;
for(const char ch:word){
if(p->node[ch-'a'] == nullptr)
p->node[ch-'a'] = new Trie();
p = p->node[ch-'a'];
}
p->isEnd = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* res = mySearch(word);
return res && res->isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
return mySearch(prefix) != nullptr;
}
private:
//Trie* root;
Trie* node[26] = {nullptr};
bool isEnd = false;
Trie* mySearch(string word){
Trie *p = this;
for (auto c : word){
if (p->node[c - 'a'] == nullptr) return nullptr;
p = p->node[c - 'a'];
}
return p;
}
};
Java
class Trie {
/** Initialize your data structure here. */
public Trie() {
}
/** Inserts a word into the trie. */
public void insert(String word) {
Trie p = this;
char[] w = word.toCharArray();
for(char ch:w){
if(p.node[ch - 'a'] == null)
p.node[ch - 'a'] = new Trie();
p = p.node[ch - 'a'];
}
p.isEnd = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
Trie res = find(word);
return res != null && res.isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
return find(prefix) != null;
}
private Trie[] node = new Trie[26];
private boolean isEnd = false;
private Trie find(String word){
Trie p = this;
char[] w = word.toCharArray();
for(char ch:w){
if(p.node[ch - 'a'] == null)
return null;
p = p.node[ch - 'a'];
}
return p;
}
}
/**
* 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 208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)(C++/Java)的更多相关文章
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
随机推荐
- Redis基础(一)——字符串、hash类型的基本使用
day09--Redis Redis介绍和安装 # Redis:软件,存储数据的,速度非常快,Redis是一个key-value存储系统(没有表的概念),cs架构的软件 服务端 客户端(python作 ...
- PS(Photoshop CC2019)安装教程
记录一下自己安装PS2019版本的安装过程~ 先获取安装资料: 百度网盘链接: 链接:https://pan.baidu.com/s/15tzmq-6JQCdVn378ZFqXJA?pwd=997y ...
- 力扣412(java)-Fizz Buzz(简单)
题目: 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == "FizzB ...
- 牛客网-SQL专项训练12
①SQL中属于分组查询的语句是?(C) 解析: A Where: 条件筛选B 联盟链: 非相关内容C Group By:分组D Having: 条件筛选 区块链大致可以分为公有链(Public Blo ...
- 从零开始入门 K8s | 调度器的调度流程和算法介绍
导读:Kubernetes 作为当下最流行的容器自动化运维平台,以声明式实现了灵活的容器编排,本文以 v1.16 版本为基础详细介绍了 K8s 的基本调度框架.流程,以及主要的过滤器.Score 算法 ...
- 基于链路思想的SpringBoot单元测试快速写法
简介:本文更偏向实践而非方法论,所提及的SpringBoot单元测试写法亦并非官方解,仅仅是笔者自身觉得比较方便.效率较高的一种写法.每个团队甚至团队内的每位开发可能都有自己的写法习惯和风格,只要能 ...
- 阿里云贾扬清:大数据+AI工程化,让数据从「成本」变为「资产」
简介: 近年来,数字经济发展迅速,企业转型背后频频涌现「数字力量」的身影.云计算.大数据.人工智能的快速融合形成了数字经济的新基建,也为数字经济发展带来了新的机遇. 5 月 20 日,阿里巴巴副总裁. ...
- 2019-9-30-dotnet-枚举当前设备wifi热点
title author date CreateTime categories dotnet 枚举当前设备wifi热点 lindexi 2019-09-30 14:42:18 +0800 2019-9 ...
- SQL server 树形递归查询
1,原始查询 原始表格查询: select * from dbo.T_DeptInfo; 原始表格查询结果: 2,递归查询 -- with 一个临时表(括号里包括的是你要查询的列名) with tem ...
- Nats集群部署
环境: 3台机器采用同样的目录名字和文件名称 服务器 192.168.10.30 192.168.10.31 192.168.10.32 nats版本2.9.15 配置文件 # 192.168.10. ...