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 ...
随机推荐
- $一步一步学Matlab(4)——使用Matlab进行初等数学运算
Matlab可以看成是一个功能强大的计算器,那么既然是计算器,进行基本的数学运算绝对是必不可少的.本文主要讲解如何用Matlab做初等数学运算,所谓"初等数学运算",可以理解成是小 ...
- 20145211 《网络渗透》MS08_067安全漏洞
20145211 <网络渗透>MS08_067安全漏洞 一.实验原理 ms08_067是服务器服务中一个秘密报告的漏洞,于2008年被发现.攻击者利用靶机默认开放的SMB服务的445端口, ...
- linux信号的介绍
1.基本概念 中断: 中断是系统对于异步事件的响应 中断信号 中断源 现场信息 中断处理程序 中断向量表 ...
- Hive常见问题
1.HQL是否区分大小写 不区分 hive> select AGE from default.studeNT; --不区分大小写,即使是表中字段 2.查看创建表过程 show create ta ...
- ubuntu18.04编译openwrt前的准备
1.获取openwrt源码 git clone https://github.com/openwrt/openwrt.git 2.安装一些库及必备程序: sudo apt-get install li ...
- rsync | scp文件同步命令使用
现在有一台服务器A,目录/data2/abc下存在若干文件夹和文件,需要复制到服务器B中.这时,可以在服务器A上执行rsync或者scp命令,将文件夹或文件复制到服务器B中. SCP: scp /da ...
- [代码解析]Mask R-CNN介绍与实现(转)
文章来源 DFann 版权声明:如果你觉得写的还可以,可以考虑打赏一下.转载请联系. https://blog.csdn.net/u011974639/article/details/78483779 ...
- Vjudge - B - 这是测试你会不会排序
2017-07-14 22:44:11 writer:pprp 题目: 他们提出的比赛规则是这样的: 1. 每方派出n个人参赛: 2. 出赛的顺序必须是从弱到强(主要担心中国人擅长的田忌赛马): ...
- Educational Codeforces Round 54 (Rated for Div. 2) DE
D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...
- DLL注入之SHELLCODE数据转换
#include "stdafx.h" #include <stdio.h> #include <string.h> #include <conio. ...