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. ...
随机推荐
- vue+mockjs模拟用户登录接口(高仿书旗)
项目demo:http://39.103.131.74:8888/shuqi
- 【数学】主成分分析(PCA)的详细深度推导过程
Based on Deep Learning (2017, MIT) book. 本文基于Deep Learning (2017, MIT),推导过程补全了所涉及的知识及书中推导过程中跳跃和省略的部分 ...
- EasyCV DataHub 提供多领域视觉数据集下载,助力模型生产
简介: 在人工智能广泛应用的今天,深度学习技术已经在各行各业起到了重要的作用.在计算机视觉领域,深度学习技术在大多数场景已经替代了传统视觉方法.如果说深度学习是一项重要的生产工具,那么数据就是不可或缺 ...
- [FE] nvm-windows: Microsoft/NPM/Google 推荐 Windows 的 Node.js 版本管理器, posix 的 nvm-sh/nvm
1. 到 github 下载 nvm-setup.zip 并安装. Releases · coreybutler/nvm-windows (github.com) 2. 安装一个版本的 nodejs. ...
- dotnet OpenXML 解析 PPT 图表 解析日期时间表示内容
在 OpenXML 里的图表存放的日期时间很多存放的是数值,需要进行转换才能获取到用户可见的字符串 日期在 OpenXML 里可以使用数值存放,需要根据数值和格式化字符串转换为用户可见的字符串 如以下 ...
- 迁移 dotnet 6 提示必须将目标平台设置为 Windows 平台
我在迁移一个古老的项目为 .NET 6 框架,但是 VS 提示 error NETSDK1136 如果使用 Windows 窗体或 WPF,或者引用使用 Windows 窗体或 WPF 的项目或包,则 ...
- 阿里云边缘容器云帮助AI推理应用快速落地
近日,阿里云技术专家徐若晨在全球分布式云大会上,分享了<边缘容器云助力AI推理高效落地>的主题演讲,分享了阿里云边缘容器云如何助力开发者实现更快速的AI推理应用的迭代和部署.此外,他还分享 ...
- Codeforces Round 940 (Div. 2) and CodeCraft-23 (A-E)
A. Stickogon 题意:给定 \(n\) 根木棒长度,问最多构成几个多边形. 贪心,四边形不会优于三角形. submission B. A BIT of a Construction 题意:构 ...
- Windows下绑定线程到指定的CPU核心
在某些场景下,需要把程序绑定到指定CPU核心提高执行效率.通过微软官方文档查询到Windows提供了两个Win32函数:SetThreadAffinityMask和SetProcessAffinity ...
- ABAP 7.58 中支持任意精度算术的新类
1. 引言 通常,有两种对编程语言的改进.第一种是让困难的事情变得简单,第二种是让不可能的事情变为可能.本文介绍的是任意精度算术,它属于第二类:使在ABAP中原本不可能的事情成为可能. 过去已经可以在 ...