Trie 树实现

Implement a trie with insert, search, and startsWith methods.

class TrieNode {
public:
// Initialize your data structure here. TrieNode *child[];
bool isWord;
TrieNode():isWord(false){
for(auto &a : child)
a = nullptr;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) return false;
p = p->child[i];
}
return p->isWord;
} // 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->child[i]) return false;
p = p->child[i];
}
return true;
} private:
TrieNode* root;
};

参看:http://www.cnblogs.com/grandyang/p/4491665.html

 

Implement Trie (Prefix Tree)的更多相关文章

  1. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  2. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  3. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  5. 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  ...

  6. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  7. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  8. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  9. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  10. Implement Trie (Prefix Tree) 解答

    Question Implement a trie with insert, search, and startsWith methods. Note:You may assume that all ...

随机推荐

  1. IOS开发之画图形

    1 画线 2 画线第二个方法  相对方法1简洁 3 矩形 4 圆 5 弧线 6画文字(略) 7 画图片(略)

  2. css常用样式

    border: border-width border-style border-color (边框属性) 全部属性 div {width:100px; height:100px; border-st ...

  3. spark发行版笔记11

    本期概览: ReceiverTracker架构设计 消息循环系统 ReceiverTracker具体的实现 Spark Streaming作为Spark Core基础 架构之上的一个应用程序,其中的R ...

  4. [WebServer] Tomcat 配置访问限制:访问白名单和访问黑名单

    前言: 昨天配置了 Tomcat 服务器运行 PHP 的环境,但是通过观察 Tomcat 这几天的日志发现,有很多莫名其妙的 IP 访问主机下莫名其妙的地址,如:/80./testproxy.php. ...

  5. pwnable.kr-flag

    题目: 使用 file 命令查看下载回来的 flag 文件,发现是一个64位的 ELF 可执行程序.通过查看,发现其具有明显的 UPX 压缩标志,所以解压之. 使用命令upx –d 进行脱壳,如果没有 ...

  6. 【mysql】关于循环插入数据 存储设计

    要求插入的数据有一定的规律 新建实例列表 CREATE TABLE users ( userId ) NOT NULL, userName ) NOT NULL, Serves ) NOT NULL, ...

  7. win10 Unistack 服务组 占用资源如何解决

    开始菜单>设置>隐私,隐私界面的左侧栏目,找最后一个“后台应用”,把后台运行的应用全部关掉即可

  8. linux中的内存申请函数的区别 kmalloc, vmalloc

    kmalloc是返回连续内存的内存分配函数 vmalloc是返回较大内存空间的,不需要连续的内存分配函数.其速度较慢,并且不能在中断上下文调用.

  9. html之页面元素印射

    首先我遇到了一个问题,尽管不是搞前端开发的但事情交到了我这里就有必要去解决. 而这个问题就是我在这边文本框输入的内容要显示在另一个文本框中其实也是非常简单.但是对于初出茅庐的新手来说就有可能会难倒他. ...

  10. bootstraps字体图标无法显示

    使用bootstraps字体图标,必须在css的同级文件夹下,建立新的文件夹为fonts,放入一下文件. 在还是无法显示字体图标的情况下,可查看bootstraps.css中的 @font-face ...