Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.

 class TrieNode{
public:
TrieNode():isWord(false)
{
memset(next,,sizeof(TrieNode*)*);
}
TrieNode(char _c):c(_c),isWord(false)
{
memset(next,,sizeof(TreeNode*)*);
}
TrieNode* next[];
char c;
bool isWord;
}; class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root=new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode* p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
p->next[id]=new TrieNode(c);
p=p->next[id];
}
p->isWord=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
return false;
p=p->next[id];
}
return p->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* p=root;
int id;
for(char c:prefix)
{
id=c-'a';
if(p->next[id]==nullptr)
return false;
p=p->next[id];
}
return true;
}
private:
TrieNode* root;
}; /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/

LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)的更多相关文章

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

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

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

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

  3. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

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

  4. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  5. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  6. Java for LeetCode 208 Implement Trie (Prefix Tree)

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

  7. leetcode@ [208] Implement Trie (Prefix Tree)

    Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...

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

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

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

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

随机推荐

  1. category extension

    1 category 分类,只能添加方法,添加的方法成为原来类的一部分,达到扩展类的目的,可以被子类继承 主要给没有源代码的类 添加方法,团队合作项目互不影响,不该动原有类的基础上添加方法 self ...

  2. Poj 1061 青蛙的约会(扩展欧几里得解线性同余式)

    一.Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  3. 使用tftp给ARM下载程序

    使用tftp给ARM下载程序 1.开发板和主机能够ping的通 前提:要把计算机的防火墙关了,不然就会出现下面这种情况 如果电脑连接的无线网,那么设置本地连接的ip设置为固定ip.Ip地址和开发的ip ...

  4. C# 自定义颜色

    一.需要引用 using System.Windows.Media; 二. 自定义颜色 通过自定义 RGB 的值来达到自定义颜色的目的 Color _Mycolor = Color.FromRgb(5 ...

  5. stm32之UCOS-III

    一.UCOS-III 学习UCOS-III,一般会学习以下内容: 任务创建.删除.挂起.恢复等: 临界区:独占CPU,尽量少用,否则会降低效率: 时间管理:时钟节拍(基于硬件定时器).软件定时器: 互 ...

  6. ObservableCollection和List的区别总结

    一.继承的类和接口,还有它们的方法不同 1)ObservableCollection比较简单,继承了Collection, INotifyCollectionChanged, INotifyPrope ...

  7. 伪分布模式 hive查询

    [root@node1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian ...

  8. 电脑MAC地址

    电脑MAC地址(Media Access Control) MAC地址是固化在网卡上串行EEPROM中的物理地址,通常有48位长.用来表示互联网上每一个站点的标识符,采用十六进制数表示. 任一网络设备 ...

  9. Vue packages version mismatch: 版本冲突;Error: EPERM: operation not permitted

    1.npm install vue-template-compiler@2.5.3 出现此问题 npm ERR! path G:\XXX.Web\node_modules\fsevents\node_ ...

  10. python之05 操作系统用户密码修改

    ubuntu的操作系统修改密码的操作方法: 一.在系统启动时按住shift键,出现下图的界面 二.按下e进入命令行,找到下图红色框中的文字并修改成rw init=/bin/bash 然后按F10启动, ...