题意:实现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. Spark Streaming揭秘 Day15 No Receivers方式思考

    Spark Streaming揭秘 Day15 No Receivers方式思考 在前面也有比较多的篇幅介绍了Receiver在SparkStreaming中的应用,但是我们也会发现,传统的Recei ...

  2. C++ STL中迭代器失效的问题

    my_container.erase(iter); 其中my_container是STL的某种容器,iter是指向这个容器中某个元素的迭代器.如果不是在for,while循环中,这种方式删除元素没有问 ...

  3. 浅谈String类型

    首先,我们要知道的是String类型是一个引用类型,它的基类是Object.并且它的内容是只读的. 我们有时候经常会看到两个字符串类型,一个是“Sting”,一个是“string”.大写的String ...

  4. C中浮点数转字符串

    求浮点数转换成字符串,如何才能获得比较正确的字符串.用printf("%f\n", (float)5); 这种方式转换出来的结果是 5.000000 ,末尾都会带6位小数. 控制精 ...

  5. 回车,根据编码获取相应记录,然后再将这录绑定到AutoList

    问题描述: 回车后,根据编码获取相应记录,然后再将这录绑定到AutoList(我们自定义控件,其实就是下拉列表),回车事件是用jquery ajax实现,这样在后台给AutoList绑定数据源,如果不 ...

  6. Ubuntu 设置root用户登录

    由于 Ubuntu 是基于 Debian 的 linux 操作系统,在默认的情况下,是没有超级用户(superuser, root)的,但有些系统操作必须有超级用户的权限才能进行,如手动释放内存等.  ...

  7. linux常用命令及安装软件命令

    1.查看操作系统是33位还是64最简单的方法 getconf LONG_BIT 或者 uname -a 2.常用命令 2.1基本操作 clear 清屏 2.2安装命令 rpm(redhat packa ...

  8. 使用Sqlserver事务发布实现数据同步

    事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进 的.这里以sqlserver2008的事务发布功能为例,对发 ...

  9. (转载)shell日志分析常用命令

    shell日志分析常用命令总结 时间:2016-03-09 15:55:29来源:网络 导读:shell日志分析的常用命令,用于日志分析的shell脚本,统计日志中百度蜘蛛的抓取量.抓取最多的页面.抓 ...

  10. office2010 office2013打开个别PPT时需要修复的解决方法

    写在前面的废话(请直接查看正文部分):一次意外之后,需要重装Microsoft office,于是屁颠屁颠就重装了一次MS office 2013,装好后发现,打开个别ppt/pptx时打不开,提示修 ...