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:
TrieNode *dic[];
// Initialize your data structure here.
TrieNode() {
for (int i = ; i < ; i++)
dic[i] = NULL;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dic[loc] == NULL)
cur->dic[loc] = new TrieNode();
cur = cur->dic[loc];
}
if (cur->dic[] == NULL)
cur->dic[] = new TrieNode();
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dic[loc] == NULL) return false;
cur = cur->dic[loc];
}
if (cur->dic[] == NULL) return false;
return true;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* cur = root;
for (int i = , len = prefix.size(); i < len; i++)
{
int loc = (int)(prefix[i] - 'a');
if (cur->dic[loc] == NULL) return false;
cur = cur->dic[loc];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

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

  1. Implement Trie (Prefix Tree) ——LeetCode

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

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

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

  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. 【LeetCode】208. Implement Trie (Prefix Tree)

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

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

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

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

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

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

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

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

随机推荐

  1. WebView的初体验

    使用安卓自带控件可以实现不通过浏览器即可上网的功能 突然就觉得安卓好强大,是不是我太无知了,太容易满足了 1.在layout中添加VebView控件 2.在Activity中设置WebView的属性 ...

  2. 巧用Windows Server 2008的NPS策略

    单位员工大部分是移动办公一族,由于病毒库更新不及时.系统补丁没有安装,使移动办公设备处于危险状态,访问内部网络时很可能威胁整个网络.该如何防守网络访问这扇门呢? 笔者所在的单位是一家传媒公司,有数百人 ...

  3. RSA进阶之共模攻击

    适用场景: 同一个n,对相同的m进行了加密,e取值不一样. e1和e2互质,gcd(e1,e2)=1 如果满足上述条件,那么就可以在不分解n的情况下求解m 原理 复杂的东西简单说: 如果gcd(e1, ...

  4. python字符串内置用法,择选重要

  5. Unable to execute dex: Multiple dex files define 问题

    今天在run公司的android project时候,报这个错误. 1. Clean Project, 重启Eclipse 没有解决. 2. 看到别人遇到的相同错误,解决方法如下: http://bl ...

  6. VS2015 +.NETMVC5 +EF实践

    -- 当做笔记,以上图片按照顺序来的. 跟着 http://www.cnblogs.com/sanshi/ 一步步来的

  7. SCU 4438 Censor(哈希+模拟栈)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...

  8. BestCoder 2nd Anniversary/HDU 5719 姿势

    Arrange Accepts: 221 Submissions: 1401 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/2 ...

  9. 后缀数组基本问题QAQ

    以下题目均来自罗穗骞的论文... No.1最长公共前缀 最长公共前缀: 题目: 给定一个字符串,询问某两个后缀的最长公共前缀. 分析: 某两个后缀的最长公共前缀就是区间height最小值,转化为RMQ ...

  10. Vijos P1404 遭遇战

    背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC ...