Implement a trie with insertsearch, 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.
bool isWord;
unordered_map<char, TrieNode*> alpha;
TrieNode():isWord(false) {}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode *p = root;
int l = word.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(word[i]) == p->alpha.end())
{
TrieNode *t = new TrieNode();
p->alpha[word[i]] = t;
}
p = p->alpha[word[i]];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode *p = root;
int l = word.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(word[i]) == p->alpha.end())
return false;
p = p->alpha[word[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;
int l = prefix.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(prefix[i]) == p->alpha.end())
return false;
p = p->alpha[prefix[i]];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

208. Implement Trie (Prefix Tree) -- 键树的更多相关文章

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

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

  2. LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)

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

  3. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  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. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

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

  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)

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

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

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

  9. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

随机推荐

  1. Javascript计算星座

    今天看群里一哥们折腾得挺热乎,手痒随便写了一个DEMO,供初学者参考. 重点,写程序先定注释,明确思路后再写具体代码. //星座定义 var constellations = [ {"Sta ...

  2. nginx中获取真实ip(转)

    原文:http://blog.csdn.net/a936676463/article/details/8961504 server { listen       80; server_name  lo ...

  3. Mysql学习笔记—时间计算、年份差、月份差、天数差(转载)

    1.获取当前日期 SELECT NOW(),CURDATE(),CURTIME(); 结果类似: 2. 获取前一天 DAY); 当前日期2018-09-17,结果: 3. 获取后一天 DAY); 当前 ...

  4. JS中手动触发事件的方法

    如果大家将一张网页看成一个form的话,大致上就成了一个web form的模型.在win form 下要想手动触发某一个对象的事件是很简单的,只要发送一条消息即可达成.(PostMessage) 但是 ...

  5. VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...

  6. Win10取消开机密码方法

    1.开始菜单输入命令“netplwiz” 2.进入到用户账户页面,选择所需账户,把“要使用本计算机,用户必须输入用户名和密码”单选框取消勾选,点击应用 3.输入密码进行 这个时候会提示输入两次该账户的 ...

  7. 《C++ Concurrency in Action》

    http://wiki.jikexueyuan.com/project/cplusplus-concurrency-action/content/resources/resource.html

  8. 20145326 《Java程序设计》第9周学习总结

    20145326 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 一.JDBC入门 1.JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数 ...

  9. FromBottomToTop第十一周项目博客

    FromBottomToTop第十一周项目博客 项目内容 塔防游戏 大体就是在地图上以合理阵型建设防御炮塔来阻止小怪进入我方阵地.玩家需用现有的金币进行炮台建设或升级,金币数可根据打怪个数增加.入侵的 ...

  10. ENC28J60

    8 KB发送接收数据包双端口 SRAM