208. Implement Trie (Prefix Tree) -- 键树
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:
// Initialize your data structure here.
bool isWord;
unordered_map<char, TrieNode*> alpha;
TrieNode():isWord(false) {}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode *p = root;
int l = word.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(word[i]) == p->alpha.end())
{
TrieNode *t = new TrieNode();
p->alpha[word[i]] = t;
}
p = p->alpha[word[i]];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode *p = root;
int l = word.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(word[i]) == p->alpha.end())
return false;
p = p->alpha[word[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;
int l = prefix.length(), i;
for(i = ; i < l; i++)
{
if(p->alpha.find(prefix[i]) == p->alpha.end())
return false;
p = p->alpha[prefix[i]];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
208. Implement Trie (Prefix Tree) -- 键树的更多相关文章
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
随机推荐
- describe neural networks as a series of computational steps via a directed graph.
https://www.microsoft.com/en-us/research/product/cognitive-toolkit/ https://github.com/microsoft/cnt ...
- 崩溃block
[__NSGlobalBlock__ setHidden:]: unrecognized selector sent to instance 0x10dbb9090(null)注释掉 sethidde ...
- sklearn学习笔记(一)——数据预处理 sklearn.preprocessing
https://blog.csdn.net/zhangyang10d/article/details/53418227 数据预处理 sklearn.preprocessing 标准化 (Standar ...
- 自己主动检測&后台复制光盘内容
原理:利用python的win32模块,注冊服务,让代码在后台执行,检測光盘并复制文件 启动的方法就是直接在cmd下,main.py install ,然后去windows 的服务下就能够看到The ...
- centos LAMP第二部分apache配置 下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转 配置apache的访问日志 配置静态文件缓存 配置防盗链 访问控制 apache rewrite 配置开机启动apache tcpdump 第二十节课
centos LAMP第二部分apache配置 下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转 配置apache的访问日志 配置静态文件缓存 配置防盗链 ...
- Elasticsearch入门教程
ElasticSearch是一个高度可扩展的开源搜索引擎并使用REST API,所以您值得拥有. 在本教程中,将介绍开始使用ElasticSearch的一些主要概念. 下载并运行ElasticSear ...
- Controller中返回数据总结(ResponseEntity,@ResponseBody,@ResponseStatus)
在传统的开发过程中,我们的控制CONTROLLER层通常需要转向一个JSP视图:但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面. ResponseEntity: ...
- python16_day35【算法】
一.BTree class BinTreeNode: def __init__(self, data): self.data = data self.lchild = None self.rchild ...
- [转][访谈]数据大师Olivier Grisel给志向高远的数据科学家的指引
原文:http://www.csdn.net/article/2015-10-16/2825926?reload=1 Olivier Grisel(OG)本人在InriaParietal工作,主要研发 ...
- Mybatis 之动态代理
使用Mybatis 开发Web 工程时,通过Mapper 动态代理机制,可以只编写接口以及方法的定义. 如下: 定义db.properties driver=oracle.jdbc.OracleDri ...