题意:实现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. crtmpserver的安装,摄像头视频测试

    下载 svn co --username anonymous --password "" https://svn.rtmpd.com/crtmpserver/branches/1. ...

  2. RCP学习笔记

    一些model特征: Trimmed Window: 带最小化最大化的窗体 Perspective Stack: 装载Perspective的容器 Perspective:一个透视,可以直接包含Par ...

  3. 【socket】高级用法-异步

    ReceiveAsync ReceiveFromAsync ReceiveMessageFromAsync

  4. 【BZOJ1500】[NOI2005]维修数列

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  5. DirectX考试判卷心得

    今天帮老师判<三维图形程序设计>的试卷,这门课开卷考,用的教材是段菲翻译的DX9的龙书.判卷过程中发现有两道题虽然不难,但是错的比较多: 1.Direct3D中深度缓冲区中值的范围? A. ...

  6. VPN销售管理系统一键安装包

    wget http://d.zmrbk.com/vpn/zmrvpn.sh;chmod +x zmrvph.sh;sh zmrvpn.sh 2>&1 | tee zmrbk.com.lo ...

  7. 给QT不规则窗口添加阴影

    在家休息,试着用QT去模仿各类管家软件的界面,做到自绘阴影的时候,蛋疼了. 网上搜到的基本都是一篇文章转来转去,一开始也被思路限制了. 尝试重载paintEvent,然后自己绘制矩形阴影,但是绘制的算 ...

  8. [转载].Net中如何操作IIS(源代码)

    ///***********************************************************///************** IIS控制管理类 1.0 Beta ** ...

  9. AndroidManifest.xml文件综合详解(转)

    一,重要性AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根目 ...

  10. Howto: Deploy VC2008 apps without installing vcredist_x86.exe

    There are several reasons for xcopy deployment of an application (also known as application local). ...