Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
实现字典树,没啥好说的。
class TrieNode {
public:
// Initialize your data structure here.
TrieNode *ch[];
bool isKey;
TrieNode() : isKey(false) {
for (auto &a : ch) a = NULL;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string s) {
TrieNode *p = root;
for (auto &a : s) {
int i = a - 'a';
if (p->ch[i] == NULL) p->ch[i] = new TrieNode();
p = p->ch[i];
}
p->isKey = true;
} // Returns if the word is in the trie.
bool search(string key) {
TrieNode *p = root;
for (auto &a : key) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return p->isKey;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *p = root;
for (auto &a : prefix) {
int i = a - 'a';
if (p->ch[i] == NULL) return false;
p = p->ch[i];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
[LeetCode] Implement Trie (Prefix Tree)的更多相关文章
-
Leetcode: Implement Trie (Prefix Tree) &;&; Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
-
[LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
-
(medium)LeetCode .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)
Description: Implement a trie with insert, search, and startsWith methods. Note:You may assume that ...
-
LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...
-
[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)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
-
字典树(查找树) 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 Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
-
《Java解惑》书摘
例子1:关于char数组的输出 System.out.println("H" + "a");//输出:Ha System.out.println('H' + ' ...
-
R 语言学习笔记
data() 调用内置数据集 help(women) 查看数据集women的帮助文档 当忘记函数名称时可以按关键字搜索:help.search(),简写为??“” str(var) 查看R对象内部结 ...
-
QT---系统托盘图标不显示原因
很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...
-
git反悔
Checkout checkout命令用于从历史提交(或者暂存区域)中拷贝文件到工作目录,也可用于切换分支. ![](./_image/2016-07-14 21-26-37.jpg?r=49) ![ ...
-
【Java每日一题】20170307
20170306问题解析请点击今日问题下方的“[Java每日一题]20170307”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...
-
admin快速搭建后台管理系统
一.基于admin后台管理系统的特点: 权限管理:权限管理是后台管理系统必不可少的部分,拥有权限管理,可以赋予用户增删改查表权限(可以分别赋予用户对不同的表有不同的操作权限): 前端样式少:后台管理主 ...
-
shell &;&; and ||
2013-04-08 17:40:47 shell中&&和||的使用方法 &&运算符: command1 && command2 & ...
-
python打开文件常见错误及解决办法
打开文件注意事项: 打开文件时需要,填写正确的路径,需要配置与文件相同的编码方式打开位机例如’utf-8‘,需要以特定 的模式打开文件 r, w,r+,w+,rb,wb,a, a+,ab等模式 f.o ...
-
java语言描述 用递归打印字符串
public class Test{ static private int n; public static void main(String[] args) { Test.n=76234; if(n ...
-
Hadoop之计数器与自定义计数器及Combiner的使用
1,计数器: 显示的计数器中分为四个组,分别为:File Output Format Counters.FileSystemCounters.File Input Format Counters和Ma ...