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. c# 公共方法

    MyMeans using System; using System.Collections.Generic; using System.Text; using System.Windows.Form ...

  2. android 导出数据库文件

    1.打开dos窗口,进入自己SDK路径下,再进入platform-tools下边 2.进入shell模式: adb shell 3.获取所有root权限: su root 4.打开需要导出的数据库文件 ...

  3. mysql 删除表

    删除表 DROP TABLE 表名;

  4. Flask系列(九)flask-script组件

    Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...

  5. weiwo.wxmmd.com将您重定向的次数过多。尝试清除 Cookie.

    折腾了很久,最后更换PHP版本解决了,我的项目用的tp3.1.2,出现上图问题时的php版本是7.1,换回5.6就没有这个问题.希望能为大家提供一个思路.

  6. 限制可编辑div只能输入纯文本

    本博客转载自张鑫旭大神的一篇文章:小tip: 如何让contenteditable元素只能输入纯文本,原文地址:http://www.zhangxinxu.com/wordpress/2016/01/ ...

  7. 001-springmvc之原理图

    1.流程. 2.时序图. 3.方法调用. (0)DispatcherServlet类.提供了HandlerMapping成员关联关系. /** MultipartResolver used by th ...

  8. http之请求报文request

    https://blog.csdn.net/blueheart20/article/details/45174399 户端发送一个HTTP请求到服务器的请求消息包括以下格式: 请求行(request ...

  9. ACM-ICPC 2017 Asia Shenyang Solution

    A: BBP Formula https://www.cnblogs.com/LzyRapx/p/7802790.html #include <bits/stdc++.h> using n ...

  10. Python3.x:基础学习

    Python3.x:基础学习 1,Python有五种标准数据类型 1.数字 2.字符串 3.列表 4.元组 5.字典 (1).数字 数字数据类型存储数字值.当为其分配值时,将创建数字对象. var1 ...