【刷题-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 ...
随机推荐
- tmux技巧
tmux 输入sz rz卡住的解决办法 解决: 仅连续4次输入ctrl+x即可解决. 原因:原因是在Xmodem协议中,ctrl+x 为信号 CAN,在协议中为"无条件中止"信号. ...
- Windows的Nginx服务可视化界面安装
先安装Nginx服务 启动 Windows安装Nginx服务 参考:https://www.cnblogs.com/pxblog/p/14871932.html 下载软件 https://github ...
- 【LeetCode】999. Available Captures for Rook 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四方向搜索 日期 题目地址:https://leetc ...
- 【九度OJ】题目1065:输出梯形 解题报告
[九度OJ]题目1065:输出梯形 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1065 题目描述: 每组测试 ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- Robin Hood
Robin Hood 题目链接 题意 给你n个人和他们的钱数,然后给你k天,每天可以从最高钱数的人那边取一块钱给最少钱数的人,问最后钱数最多的人和钱数最少的人相差多少: 思路 二分最钱数,能下降到的位 ...
- 【剑指Offer】第一个只出现一次的字符 解题报告(Python)
[剑指Offer]第一个只出现一次的字符 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-intervie ...
- 《机器学习实战》kNN算法及约会网站代码详解
使用kNN算法进行分类的原理是:从训练集中选出离待分类点最近的kkk个点,在这kkk个点中所占比重最大的分类即为该点所在的分类.通常kkk不超过202020 kNN算法步骤: 计算数据集中的点与待分类 ...
- 「THUSCH 2017」大魔法师
Description 大魔法师小 L 制作了 \(n\) 个魔力水晶球,每个水晶球有水.火.土三个属性的能量值.小 L 把这 \(n\) 个水晶球在地上从前向后排成一行,然后开始今天的魔法表演. 我 ...
- vue项目在ie浏览器下报语法错误
错误如下: 1.用vue 写的项目最多可以兼容到IE9 及以上版本 2.使用babel-polyfill做兼容npm install babel-polyfill --save-dev 安装之后如果发 ...