【LeetCode】208. Implement Trie (Prefix Tree)
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.
一个字母代表一个子树,因此为26叉树,end标记表示是否存在以该字母为结尾的字符串。
class TrieNode {
public:
TrieNode* children[];
bool end;
// Initialize your data structure here.
TrieNode() {
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size())
{
if(curNode->end == true)
// exist
return;
else
// insert
curNode->end = true;
}
else
{
while(i < word.size())
{
curNode->children[word[i]-'a'] = new TrieNode();
curNode = curNode->children[word[i]-'a'];
i ++;
}
curNode->end = true;
}
}
// Returns if the word is in the trie.
bool search(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size() && curNode->end == true)
// curNode must be leaf
return true;
else
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i = ;
TrieNode* curNode = root;
while(i < prefix.size() && curNode->children[prefix[i]-'a'] != NULL)
{
curNode = curNode->children[prefix[i]-'a'];
i ++;
}
if(i == prefix.size())
// curNode might be left or not
return true;
else
return false;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

【LeetCode】208. Implement Trie (Prefix Tree)的更多相关文章
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【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 Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- [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 a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 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 ...
- leetcode@ [208] Implement Trie (Prefix Tree)
Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...
随机推荐
- android 数据加密——加密的概述
数据加密又称密码学,它是一门历史悠久的技术,指通过加密算法和加密密钥将明文转变为密文,而解密则是通过解密算法和解密密钥将密文恢复为明文.数据加密目前仍是计算机系统对信息进行保护的一种最可靠的办法.它利 ...
- 【Eclipse】Eclipse性能调优
Eclipse性能调优 eclipse 吃内存_百度搜索 eclipse 性能调优之内存分配 - Defonds 的专栏 - CSDN博客 优化JVM参数提高eclipse运行速度 - Java综合 ...
- Chromium网页Layer Tree创建过程分析
在Chromium中.WebKit会创建一个Graphics Layer Tree描写叙述网页.Graphics Layer Tree是和网页渲染相关的一个Tree. 网页渲染终于由Chromium的 ...
- MFC如何获取硬盘的序列号
要把如下的两篇文章结合起来看: qt怎么获取硬盘序列号,是不是没戏? http://www.qtcn.org/bbs/simple/?t65637.html system("wmic pat ...
- CareerCup Facebook Total number of substring palindrome
Write a function for retrieving the total number of substring palindromes. For example the input is ...
- Direct2D教程VII——变换几何(TransformedGeometry)对象
目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...
- 关于Puppet不得不说的故事
Puppet对于做DevOps的同学来说,是个熟悉的名字,但仍有许多人并不了解它.那么我先来简单介绍一下:Puppet是由Puppetlabs公司开发的系统管理框架和工具集,被用于IT服务的自动化管理 ...
- C/C++——程序的内存分配
C/C++程序内存分配 一.预备知识-程序的内存分配 一个由c/C++编译的程序占用的内存分为下面几个部分 1.栈区(stack):由编译器自己主动分配释放 ,存放函数的參数值,局部变量的值等.其操作 ...
- angular5中使用jsonp请求页面
说多了,都是眼泪,折腾了很久,各种百度,查到的例子,全都报错,可能是因为我的angular的版本太高,向下都不兼容? 我的angular版本为最新的5.2.11: 下面是正确的jsonp请求的方法 1 ...
- 如何设置Apache中的最大连接数
Apache的主要工作模式有两种:prefork和worker 一.两种模式 prefork模式(缺省模式) prefork是Unix平台上的默认(缺省)MPM,使用多个子进程,每个子进程只有一个线程 ...