题意:实现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. java 中的equal和"=="

    先看一段代码 String str1 = new String("str"); String str2 = new String("str"); System. ...

  2. zxing-master core编译

    1.下载 Maven: http://maven.apache.org/download.cgi 2.下载完以后,设一下系统变量:MAVE_HOME 3.Path中加入%MAVEN_HOME%\bin ...

  3. easy ui 异步上传文件,跨域

    easy ui 跨域上传文件,代码如下: 1.html代码:(这段代码是个win窗体,我在点击上传图片按钮然后弹出一个上传图片的窗体,选择图片再进行上传,这样在form提交时,提交的参数会少一点.) ...

  4. 【BZOJ 1877】 [SDOI2009]晨跑

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...

  5. uxpin books

    http://uxpin.com/knowledge.html/?utm_source=Email+Marketing+Automation&utm_campaign=80d94e146a-l ...

  6. bnuoj 1053 EASY Problem (计算几何)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1053 [题意]:基本上就是求直线与圆的交点坐标 [题解]:这种题我都比较喜欢用二分,三分做,果然可以 ...

  7. 1020: [SHOI2008]安全的航线flight - BZOJ

    Description在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞机 ...

  8. 为Dapper编写一个类似于EF的Map配置类

    引言 最近在用Dapper处理Sqlite.映射模型的时候不喜欢用Attribute配置,希望用类似EF的Map来配置,所以粗略的实现了一个. 实现 首先是主体的配置辅助类型: using Syste ...

  9. SQLite数据库简介(转)

    大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...

  10. 让wordpress投稿作者在后台只看到自己的文章

    wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...