【刷题-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 ...
随机推荐
- input type 使用
type属性值 hidden: 隐藏. text:文本 search:搜索 tel url email password:密码 date:日期选择器 month:月份选择器 week:周选择器 tim ...
- UDP&串口调试助手用法(5)
note 提供安装包 基于win10开发 已通过win10测试,windows其他平台,没有测试 日志 2021-09-18 1.修复计算校验和错误的现象 2.屏蔽不计算校验和位置的REG验证(后期更 ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1025. Divisor Game 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 动态规划 日期 题目地址:https://l ...
- 【LeetCode】500. Keyboard Row 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...
- 【九度OJ】题目1192:回文字符串 解题报告
[九度OJ]题目1192:回文字符串 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1192 题目描述: 给出一个长度不超过1000的 ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- D. Water Tree
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- Travelling(hdu3001)
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...