【刷题-LeetCode】208. Implement Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
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.
解

class trie_node{
public:
vector<trie_node*>child; // 根据当前字符选取下一个节点,如果当前字符是a,接下来就往child['a']的分支走
bool isWord; // 表明到达该节点时,是否包含了一个完整的单词
// 构造函数
trie_node(): isWord(false), child(26, NULL){}
// 析构函数
~trie_node(){
for(auto &c : child)delete c;
}
};
class Trie {
public:
trie_node* root;
/** Initialize your data structure here. */
// 构造函数
Trie() {
root = new trie_node();
}
/** Inserts a word into the trie. */
void insert(string word) {
trie_node *cur = root;
for(int i = 0; i < word.size(); ++i){
int idx = word[i] - 'a';
if(cur->child[idx] == NULL){
cur->child[idx] = new trie_node();
}
cur = cur->child[idx];
}
cur->isWord = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
trie_node *cur = root;
for(auto ch : word){
int idx = ch - 'a';
if(cur->child[idx] == NULL)return false;
cur = cur->child[idx];
}
return cur->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
trie_node *cur = root;
for(auto ch : prefix){
int idx = ch - 'a';
if(cur->child[idx] == NULL)return false;
cur = cur->child[idx];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
【刷题-LeetCode】208. Implement Trie (Prefix Tree)的更多相关文章
- 字典树(查找树) 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刷题笔记]Implement Trie (Prefix Tree)
题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- Java for LeetCode 208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- leetcode@ [208] Implement Trie (Prefix Tree)
Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...
- LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are ...
- 【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) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
随机推荐
- CF981B Businessmen Problems 题解
Content 有一个长度为 \(n\) 的序列和长度为 \(m\) 的序列,两个序列中的元素都有一个编号 \(num\) 和一个值 \(val\),且同一个序列的元素之间的编号互不相同.现在从这两个 ...
- CountDownLatch源码阅读
简介 CountDownLatch是JUC提供的一个线程同步工具,主要功能就是协调多个线程之间的同步,或者说实现线程之间的通信 CountDown,数数字,只能往下数.Latch,门闩.光看名字就能明 ...
- nim_duilib(16)之xml学习实战(GTAV加载窗口实现)
本文的目标 使用配置xml实现下面的结果 布局 整体采用水平布局,左边显示文字区域设置为垂直布局. lets go stage 1 创建一个空白窗体,并设置为半透明:同时,使得整个窗口可以移动,则 将 ...
- visual studio c++项目文件分类混乱整理
演示环境: win10 + vs2015 (下面简称VS)+ visual assist (下面简称VA) 1.混乱 装了VA的VS,有个快捷键,可快速切换 .h 文件和 .cpp(.cc, .cxx ...
- CHARINDEX 用法
CHARINDEX 返回字符串中指定表达式的起始位置. 语法 CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数 expre ...
- 【操作系统】bat文件 系统找不到文件路径
我直接使用bat文件发现我要删除的文件夹还在 在bat文件最后添加pause指令查看发现报错:系统找不到文件路径 原因:路径包括中文,显示乱码(因为txt另存为bat时用量utf-8编码) 解决方法: ...
- Normalization Methods
目录 概 主要内容 Batch Normalization Layer Normalization Instance Normalization Group Normalization Ioffe S ...
- 定义制造业操作(定义 MES/MOM 系统)
定义制造业操作(定义 MES/MOM 系统) 制造业操作包含众多工厂级活动,涉及设备(定义.使用.时间表和维护).材料(识别.属性.位置和状态).人员(资格.可用性和时间表),以及这些资源与包含其信息 ...
- CS5211替代兼容PS8625|普瑞PS8625替代方案|CapstoneCS5211
PS8625是一个DP显示端口 到LVDS转换器芯片,利用GPU和显示端口(DP) 或嵌入式显示端口(eDP) 输出和接受LVDS输入的显示面板.PS8625实现双通道DP输入,双链路LVDS输出.P ...
- Java中的对象、类、抽象类、接口的理解
1.对象 对象是个具体的东西,有着明确的属性特征和行为特征. 例如:你手上牵着的女朋友或男朋友就是个具体的对象. 如图中的苏格兰折耳猫,特征是耳朵向下呈折叠状,有着具体的颜色(你看到的颜色),并且和其 ...