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. css选择符

    E>F:子选择符,选择所有作为E元素的子元素F.<style type="text/css">li>a {color: #ccc;}</style&g ...

  2. 记一次酷派尚锋Y75刷机

    昨天因为手机卡的原因,我的同学帮他的同学刷机,听他说是用刷机精灵线刷的rom包,但是刷机失败了,就来找我把弄好,他是个半吊子水平,刚接触这个东西,也是运气不好,什么刷机失败的问题都让他遇上了,刷了几个 ...

  3. hibernate入门实例

    1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...

  4. 解决 git 提交文件提示 Filename too long 问题

    1.git config --system core.longpaths true 2.git config core.longpaths true

  5. iOS中文网址路径转换URLEncode

    如果返回的URL中有中文可以用此方法转换 今天发现一个蛋疼的问题,服务端返回的urlString里面有时含有中文,使用 [NSURL URLWithString:urlString]生成URL对象时, ...

  6. 基于UDP协议模拟的一个TCP协议传输系统

    TCP协议以可靠性出名,这其中包括三次握手建立连接,流控制和拥塞控制等技术.详细介绍如下: 1. TCP协议将需要发送的数据分割成数据块.数据块大小是通过MSS(maximum segment siz ...

  7. Eclipse插件开发中对于Jar包和类文件引用的处理(彻底解决插件开发中的NoClassDefFoundError问题)(转)

    目的:Eclipse插件开发中,经常要引用第三方包或者是引用其他插件中的类,由于插件开发环境引用类路径的设置和运行平台引用类路径的设置不同,经常导致开发过程OK,一旦运行则出现NoClassDefFo ...

  8. android app 提示信息

    Toast.makeText(this,"You cannot have less than 1 coffee",Toast.LENGTH_SHORT).show();TextVi ...

  9. 多线程下的for循环问题

    List<int> _ValueLis = new List<int>(); private void AddInt(int i) { _ValueLis.Add(i); } ...

  10. python登录执行命令

    #-*- coding: utf-8 -*- #!/usr/bin/python import paramiko import threading import getpass def ssh2(ip ...