Trie 树实现

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

class TrieNode {
public:
// Initialize your data structure here. TrieNode *child[];
bool isWord;
TrieNode():isWord(false){
for(auto &a : child)
a = nullptr;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) return false;
p = p->child[i];
}
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;
for(auto &a : prefix){
int i = a - 'a';
if(!p->child[i]) return false;
p = p->child[i];
}
return true;
} private:
TrieNode* root;
};

参看:http://www.cnblogs.com/grandyang/p/4491665.html

 

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

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

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

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

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

  3. [LeetCode] 208. 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) 、211. Add and Search Word - Data structure design

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

  5. 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  ...

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

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

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

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

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

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

  9. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  10. Implement Trie (Prefix Tree) 解答

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

随机推荐

  1. Scala 并发编程

    Runnable/Callable 线程 Executors/ExecutorService Futures 线程安全问题 例子:搜索引擎 解决方案 Runnable/Callable Runnabl ...

  2. (转)spring boot注解 --@EnableAsync 异步调用

    原文:http://www.cnblogs.com/azhqiang/p/5609615.html EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. @Co ...

  3. 打造最高效的科研环境之Emacs插件们

    0 盲人摸象 作为初学者,迫切的需求就是直接上手Emacs并打造包含自动补全命令在内的科研环境. 和网上众多的插件安装的教程相比,我认为找到一个与自己需求匹配的Emacs配置环境来得更方便. 本例中, ...

  4. hadoop运行原理之Job运行(四) JobTracker端心跳机制分析

    接着上篇来说,TaskTracker端的transmitHeartBeat()方法通过RPC调用JobTracker端的heartbeat()方法来接收心跳并返回心跳应答.还是先看看这张图,对它的大概 ...

  5. App Store最新审核标准,中文版

    App store最新审核标准(2015.3)公布 1. 条款和条件 1.1 为App Store开发程序,开发者必须遵守 Program License Agreement (PLA).人机交互指南 ...

  6. Android中Activity的生命周期

    简介: 这个基本是必问的问题了,说一下你对Activity生命周期的理解,呵呵… onCreate, onStart, onResume, onPause, onStop, onDestroy, on ...

  7. 转-Apache的Order Allow,Deny 详解

    Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是:Or ...

  8. android 取消edittext焦点

    页面中如果有EditText会默认获取焦点,如果进入页面时不想让其获取到焦点可以按如下步骤: 1.在布局的最外层添加属性: android:focusable="true" and ...

  9. win10 内测14352 加入了容器 和docker新功能,想体验的赶快升级

    原来只在server2016上有,现在加入到win0内测版了windows 容器提供了两种级别的隔离技术,分别是Windows Server container  和Hyper-V Container ...

  10. Common.Logging log4net Common.Logging.Log4Net 配置

    1.log4net 单独配置 log4net支持多种格式的日志输出,我这里只配置输出到本地的txt文件这种格式. <log4net> <root> <appender-r ...