LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert
, search
, and startsWith
methods.
实现字典树,前面好像有道题做过类似的东西,代码如下:
class TrieNode {
public:
// Initialize your data structure here.
TrieNode():isLeaf(false)
{
for(auto & t : dic){
t = NULL;
}
}
TrieNode * dic[];
bool isLeaf;
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode * p = root;
for(int i = ; i < word.size(); ++i){
int index = word[i] - 'a';
if(p->dic[index] == NULL)
p->dic[index] = new TrieNode();
p = p->dic[index];
}
p->isLeaf = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode * p = root;
for(int i = ; i < word.size(); ++i){
int index = word[i] - 'a';
if(p->dic[index] == NULL)
return false;
p = p->dic[index];
}
return p->isLeaf;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode * p = root;
for(int i = ; i < prefix.size(); ++i){
int index = prefix[i] - 'a';
if(p->dic[index] == NULL)
return false;
p = p->dic[index];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
LeetCode OJ: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] 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面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 【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: ...
- 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 ...
随机推荐
- hadoop 一些命令
关闭访问墙 service iptables stop hadoop dfs -mkdir input hadoop dfs -copyFromLocal conf/* input hadoop j ...
- 《Java程序设计》实验1实验报告
20145318 <Java程序设计>实验1实验报告 实验题目 通过对500个数据进行操作,实现快速排序.选择排序.直接插入排序算法时间复杂度的比较:并在排序数据中快速查找某一数据,给出查 ...
- 在Linux终端管理文件你要知道的11个命令
LS - 列表文件 ls命令列出目录中的文件. 默认情况下,使用ls列出当前目录下的文件. 2 你也可以列出文件递归-也就是说,列出所有文件在当前目录中的目录-使用ls -R.LS还可以列出在其他目录 ...
- 聊一聊HTML <!--…-->标签
定义 注释标签用于在html源代码中插入注释.注释不会在浏览器上显示. 用法 根据定义的基本用法,代码如下 <!-- 这是一段注释,我不会显示在页面上 --> 浏览器的支持情况 所有浏览器 ...
- mysql的一些基本知识
一.数据类型: 字符型 整型 浮点型 日期时间型 二.数据表操作: 插入记录:INSERT 表名(···,···,···) VALUES('···','···',···): 查找记录:SELECT ...
- JQuery中serialize()
一.serialize()定义和用法: serialize()方法通过序列化表单值,创建标准的URL编码文本字符串,它的操作对象是代表表单元素集合的jQuery 对象.你可以选择一个或多个表单元素(比 ...
- ng2 quickstart-primeng
1.导入quickstart-angular项目 2.安装primeng npm install primeng 3.安装@angular/animations npm install @angula ...
- HDU 5763 Another Meaning(DP+KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
- 解决ubuntu在当前位置打开终端功能
ubuntu右键在当前位置打开终端 ubuntu增加右键命令: 在终端中打开 软件中心: 搜索nautilus-open-terminal安装 命令行: sudo apt-ge ...
- java 资源文件夹下的MEAT-INF
META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗 https://www.cnblogs.com/demingblog/p/5653844.html Jar ...