class TrieNode {
public:
const static int NR_FANOUT = ;
TrieNode* child[NR_FANOUT];
int count;
// Initialize your data structure here.
TrieNode() {
for (int i=; i<NR_FANOUT; i++) {
child[i] = NULL;
}
count = ;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string s) {
insert(s, , root);
} // Returns if the word is in the trie.
bool search(string key) {
return search(key, , root) != NULL;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
return search_prefix(prefix, , root) != NULL;
}
private:
TrieNode* insert(string& s, int pos, TrieNode* current) {
int len = s.size();
if (pos > len) {
return NULL;
}
if (current == NULL) {
current = new TrieNode();
}
if (pos == len) {
current->count++;
return current;
}
int idx = s[pos] - 'a';
current->child[idx] = insert(s, pos + , current->child[idx]);
return current;
} TrieNode* search(string& s, int pos, TrieNode* current) {
if (current == NULL) {
return NULL;
}
int len = s.size();
if (pos > len) {
return NULL;
} else if (pos == len && current->count) {
return current;
}
return search(s, pos + , current->child[s[pos] - 'a']);
}
TrieNode* search_prefix(string& s, int pos, TrieNode* current) {
if (current == NULL) {
return NULL;
}
int len = s.size();
if (pos >= len) {
return current;
}
return search_prefix(s, pos + , current->child[s[pos] - 'a']);
}
private:
TrieNode* root;
};

mplement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

LeetCode Tries 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 a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

随机推荐

  1. gulp-load-task 解决 gulpfile.js 过大的问题

    当我们在项目中使用gulp来实现前端自动化时,常常因任务太多导致gulpfile.js越来越臃肿,增加后期维护/变更成本.在计算机科学领域中,分治可以将我们的项目变得井然有序.所以,我们利用这个理念, ...

  2. Alamofire源码导读三:返回的处理逻辑

     以DataRequest 为例子. 最简单的返回 URLSession 有一个方法,可以构建 URLSessionDataTask func dataTask(with url: URL, com ...

  3. centos7修改默认运行级别的变化

    在学习centos7,视频教学中使用的的centos6,查看权限文件命令为 cat /etc/inittab/ 但是在centos7中输入以下命令会出现这种情况 这个已经不用了,现在修改权限的命令修改 ...

  4. css四种选择器总结

    css 在网页开发中扮演着重要的角色,被誉为网页开发的三剑客,如果说html是人的外在器官部分,那css无疑是各个器官组成在一起然后表现出来,css又称样式重叠在网页排版布局中的地位举足轻重.   做 ...

  5. 【ARC072F】 Dam 单调队列

    题目大意: 有一个水库,容量为$L$,一开始是空的.有$n$天. 对于第i天,每天早上有$v_i$单位的,水温为$t_i$的水流进来.每天晚上你可以放掉一些水,多少自定.但是必须保证第二天水库不会溢出 ...

  6. C# 连接Paradox DB

    Paradox数据库是一个成名于15年前的数据库,那时候Borland公司还存在.最近客户提出需求,要在一套用了12年+的应用程序上作些功能更改.这套应用程序使用Delphi+Paradox数据库. ...

  7. 搭建互联网架构学习--003--maven以及nexus私服搭建

    跳过,等待完善中,,, 后台服务工具maven:使用Nexus配置Maven私有仓库 一.安装配置Nexus 1. 下载nexus https://www.sonatype.com/download- ...

  8. 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式

    1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...

  9. lintcode-->翻转字符串

    给定一个字符串,逐个翻转字符串中的每个单词. 您在真实的面试中是否遇到过这个题? Yes 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包 ...

  10. python 笔记-转

    python笔记   Python 学习笔记 - 14.技巧(Tips)   Python 学习笔记 - 13.异常(Exception)   Python 学习笔记 - 12.流程控制(Contro ...