208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法。
注意:
你可以假设所有的输入都是小写字母 a-z。
详见:https://leetcode.com/problems/implement-trie-prefix-tree/description/
Java实现:
Trie树,又称为字典树、单词查找树或者前缀树,是一种用于快速检索的多叉数结构。例如,英文字母的字典树是26叉数,数字的字典树是10叉树。
Trie树的基本性质有三点,归纳为:
根节点不包含字符,根节点外每一个节点都只包含一个字符。
从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
每个节点的所有子节点包含的字符串不相同。
insert:分析单词的每一个字符,如果存在与已经有的孩子中,则循环转到该孩子节点,否则新建孩子节点,最后在单词的末尾字符isWord标志位true;
search: 逐个分析每个字符,如果不存在该字符的孩子则返回false,否则循环之后,查看末尾字符的标志位即可;
prefix: 和search一样,只是在最后只要是都存在每个字符相应的孩子map,则返回true。
class TrieNode{
boolean isWord;
HashMap<Character,TrieNode> map;
public TrieNode(){
map=new HashMap<Character,TrieNode>();
}
}
class Trie {
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root=new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
char[] charArray=word.toCharArray();
TrieNode tmp=root;
for(int i=0;i<charArray.length;++i){
if(!tmp.map.containsKey(charArray[i])){
tmp.map.put(charArray[i],new TrieNode());//添加
}
tmp=tmp.map.get(charArray[i]);//转到孩子节点
if(i==charArray.length-1){//末尾字符
tmp.isWord=true;
}
}
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode tmp=root;
for(int i=0;i<word.length();++i){
TrieNode next=tmp.map.get(word.charAt(i));
if(next==null){
return false;
}
tmp=next;
}
return tmp.isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode tmp=root;
for(int i=0;i<prefix.length();++i){
TrieNode next=tmp.map.get(prefix.charAt(i));
if(next==null){
return false;
}
tmp=next;
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
C++实现:
class TrieNode
{
public:
TrieNode *next[26];
char c;
bool isWord;
TrieNode():isWord(false)
{
memset(next,0,sizeof(TrieNode*)*26);
}
TrieNode(char _c):c(_c),isWord(false)
{
memset(next,0,sizeof(TrieNode*)*26);
}
};
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root=new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode *p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
{
p->next[id]=new TrieNode(c);
}
p=p->next[id];
}
p->isWord=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode *p=root;
int id;
for(char c:word)
{
id=c-'a';
if(p->next[id]==nullptr)
{
return false;
}
p=p->next[id];
}
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 id;
for(char c:prefix)
{
id=c-'a';
if(p->next[id]==nullptr)
{
return false;
}
p=p->next[id];
}
return true;
}
private:
TrieNode *root;
}; /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/
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 ...
- 字典树(查找树) 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) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 208. Implement Trie (Prefix Tree) -- 键树
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
随机推荐
- 让你的 EditText 所有清除
原文地址:让你的 EditText 所有清除 參考原文:Giving your Edit Texts the All Clear 项目地址(欢迎 Star):ClearEditText 在输入文本的时 ...
- python 多线程中同步的小样例
#!/usr/bin/python # -*- coding: UTF-8 -*- # 在一个资源池中.获取资源 # Author: zhang # Date: 2015-7-27 import ti ...
- Linux登录自动切换root账户与历史命令优化
1:当我们Linux系统优化完成,会使用oldboy用户远程连接CRT登录,每次连接都需要使用sudo su - 或者su - 输入密码登录,请问如何在CRT连接的时候自动的切换到root账户,(提示 ...
- Web—CSS概述
一.概念: 它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言 的一个子集)等文件样式的计算机语言. 二.特点: 1.实现网页内容与样式的分离 2.降低图形文件的 ...
- MySQL运行计划不准确 -概述
为毛 MySQL优化器的运行计划 好多时候都不准确,不是最优的呢(cpu+io)??? 因素太多了:: 存在information_schema的信息是定期刷新上去的,好多时候不是最真的,甚至相差好大 ...
- button和submit区别
type=button 就单纯是按钮功能 type=submit 是发送表单 但是对于从事WEB UI的人应该要注意到,使用submit来提高页面易用性: 使用submit后,页面 ...
- spring依赖注入(反转控制)
SPRING依赖注入机制(反转控制)解析 Spring能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的 Service对象,还是持久层的DAO对象,都可在Spring的 ...
- docker启动centos容器后如何用putty连接
在前面的文章中,我提到过,win10 docker启动容器时,只有配置了宿主机和docker容器的端口映射,外部应用才能访问到容器中的服务,比如映射到Nginx的80端口.现在我将宿主机的某个端口映射 ...
- 20170111 ABAP技术小结(全半角转换)
DATA: it_po LIKE it_alv OCCURS 0 WITH HEADER LINE.************************************************** ...
- POI中HSSF和XSSF操作Excel
POI中HSSF和XSSF操作Excel 在公司实习快一个月了,这段时间公司业务要用JAVA操作复杂的Excel报表.刚开始的Excel还好,没有涉及到复杂的图表,所以使用JXL操作Excel,但 ...