题意:实现trie树的3个功能,只含小写字母的串。

思路:老实做即可!

 class TrieNode {
public:
TrieNode* chd[];
bool flag;
// Initialize your data structure here.
TrieNode() {
memset(chd,,sizeof(chd));
flag=;
} }; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
int p=;
TrieNode* tmp=root;
while(p<word.size())
{
if(!tmp->chd[word[p]-'a'])
{
TrieNode* newnode=new TrieNode();
tmp->chd[word[p]-'a']=newnode;
}
tmp=tmp->chd[word[p]-'a'];
p++;
}
tmp->flag=;
} // Returns if the word is in the trie.
bool search(string word) {
int p=;
TrieNode* tmp=root;
while(p<word.size())
{
//cout<<word[p]<<" ";
if(tmp->chd[word[p]-'a']) tmp=tmp->chd[word[p]-'a'];
else return false;
p++;
}
if(tmp->flag==) return true;
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int p=;
TrieNode* tmp=root;
while(p<prefix.size())
{
if(tmp->chd[prefix[p]-'a']) tmp=tmp->chd[prefix[p]-'a'];
else return false;
p++;
}
return true;
} private:
TrieNode* root;
};

AC代码

LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)的更多相关文章

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

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

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

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

  3. [LeetCode] 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) 实现字典树(前缀树)

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

  5. Implement Trie (Prefix Tree)实现字典树

    [抄题]: Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inp ...

  6. Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

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

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

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

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

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

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

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

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

随机推荐

  1. Linux进程间通信IPC学习笔记之同步一(线程、互斥锁和条件变量)

    基础知识: 测试代码: 参考资料: Posix 多线程程序设计

  2. OFBiz进阶之HelloWorld(五)创建新实体

    参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guid ...

  3. Mac OS X 10.10 Yosemite PHP 5.5.14 free type support fix

    通过将php将至5.4来勉强支持freetype扩展,不推荐此方法 after upgrading to new Mac OS X Yosemite, i noticed that free type ...

  4. Xcode文档下载与安装路径

    https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex ~/Library/Developer/ ...

  5. jquery pass parameter to ajax callback

    $('.del').on('click', function () { var id = $(this).attr('id'); var url = '/m/g2_content_del/' + id ...

  6. C# 的轻量级 RPC 框架

    Redola.Rpc 的一个小目标 Redola.Rpc 的一个小目标 Redola.Rpc 的一个小目标:20000 tps. Concurrency level: 8 threads Comple ...

  7. WebUploader API

    Uploader new Uploader( opts ) ⇒ Uploader 上传入口类. var uploader = WebUploader.Uploader({ swf: 'path_of_ ...

  8. ashx与验证码

    using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; us ...

  9. swift基础--运算符

    (1)加减乘除 (2)三目运算,切记后面的空格如果不加会报错的.估计是苹果的bug. (3)聚合运算符,省却了一个判断,很人性化 (4)区间运算符 //加减乘除等等 let a = 2 let b = ...

  10. 记一次apt-get无法安装git的问题

    解决apt-get安装过程中出现的Size mismatch和Hash Sum mismatch问题. 事情起因 我从单位复制了一个Virtualbox虚拟机(ubuntu 15.04 Desktop ...