Implement a trie with insert, search, and startsWith methods.

Note:

You may assume that all inputs are consist of lowercase letters a-z.

Hide Tags Data Structure Trie

实现一棵Trie树以及实现查询的功能,依据上一篇文章中的分析和伪代码能够非常迅速地实现:

runtime:68ms

class TrieNode {
public:
// Initialize your data structure here.
TrieNode() {
words=0;
prefixs=0;
for(int i=0;i<26;i++)
edges[i]=NULL;
}
int words;
int prefixs;
TrieNode* edges[26];
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
insertHelper(root,word,0);
} // Returns if the word is in the trie.
bool search(string word) {
return searchHelper(root,word,0)!=0;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
return startsWithHelper(root,prefix,0)!=0;
} void insertHelper(TrieNode * node,string &word,int pos) {
if(pos==word.size())
{
node->words++;
node->prefixs++;
}
else
{
node->prefixs++;
int char_code=word[pos]-'a';
if(node->edges[char_code]==NULL)
node->edges[char_code]=new TrieNode();
insertHelper(node->edges[char_code],word,pos+1);
}
} int searchHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->words;
else if(node->edges[char_code]==NULL)
return 0;
else
return searchHelper(node->edges[char_code],word,pos+1);
} int startsWithHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->prefixs;
else if(node->edges[char_code]==NULL)
return 0;
else
return startsWithHelper(node->edges[char_code],word,pos+1);
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

LeetCode208:Implement Trie (Prefix Tree)的更多相关文章

  1. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. vim之<F12> 一键生成tags的一些小优化

    在之前我写的<<vim之tags>>中最后提到将vim和tags成和更新的全部集中到一个<f12>键上来. 这在实践中证明是相当方便的, 不过依然村庄几个问题如下: ...

  2. [入门指南]-技术学习-Ebean

    占坑 官网

  3. DECLARE_MESSAGE_MAP( )

    DECLARE_MESSAGE_MAP( ) 说明: 你的程序中的每一个CCmdTarget的派生类都可以提供一个消息映射以处理消息.在你的类声明的末尾使用DECLARE_MESSAGE_MAP宏.然 ...

  4. SOUI界面库 添加 windows系统文件图标皮肤

    最近在学习soui界面库.其中有用到SListCtrl这个控件来现在文件信息.控件用法基本上和mfc 的CListCtrl差不多.也支持图标显示.但是图标是要自己加入图标图片的.这个就有点不好弄.于是 ...

  5. discourse论坛迁移

    在源设备的操作备份数据文件tar -czvf discoursefile716.tar.gz /var/discourse然后把此discoursefile716.tar.gz文件传到需要迁移的设备上 ...

  6. Python模块 os.walk

    Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...

  7. Django-搭建win7虚拟环境-virtualenv

    为什么要配置Django虚拟环境? 例如:在开发Python Django的时候,系统安装的Python3只有一个版本:3.6.所有第三方的包都会被pip安装到Python3的site-package ...

  8. 20170704-WNDR4300串口连接信息

    find_hif: bootstrap = 0xaf055aWASP BootROM Ver. 1.1Nand Flash initONFI: Control setting = 0xb44find_ ...

  9. 关于单片机编程里面调用sprintf死机的解决方法及原因分析

    好久之前的做的笔记,这里贴出. char String[100];//直接用数组代替指针即可解决 下面代代码下载至单片机中,发现会出现单片机死机问题 #include "stdio.h&qu ...

  10. linux修改mysql表结构

    增加字段: alter table [tablename] add [字段名] [字段类型] first(首位); alter table [tablename] add [字段名] [字段类型] a ...