208. 实现 Trie (前缀树)
主要是记录一下这个数据结构。


比如这个trie树,包含三个单词:sea,sells,she。
代码:
class Trie {
bool isWord;
vector<Trie*> children;
public:
/** Initialize your data structure here. */
Trie() {
isWord=false;
children.assign(,nullptr);
}
/** Inserts a word into the trie. */
void insert(string word) {
if(word.empty()){return;}
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
cur->children[word[i]-'a']=new Trie();
}
cur=cur->children[word[i]-'a'];
}
cur->isWord=true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
return false;
}
cur=cur->children[word[i]-'a'];
}
return cur->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
auto cur=this;
for(int i=;i<prefix.size();++i){
if(cur->children[prefix[i]-'a']==nullptr){
return false;
}
cur=cur->children[prefix[i]-'a'];
}
return true;
}
};
/**
* 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. 实现 Trie (前缀树)的更多相关文章
- Java实现 LeetCode 208 实现 Trie (前缀树)
208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...
- 力扣 - 208. 实现Trie(前缀树)
目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...
- [leetcode] 208. 实现 Trie (前缀树)(Java)
208. 实现 Trie (前缀树) 实现Trie树,网上教程一大堆,没啥可说的 public class Trie { private class Node { private int dumpli ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 力扣208——实现 Trie (前缀树)
这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...
- 4.14——208. 实现 Trie (前缀树)
前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...
- 力扣208. 实现 Trie (前缀树)
原题 以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie 1 class Trie: 2 3 def __init__(self): 4 ""&qu ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
随机推荐
- Node.js核心模块-http
通过node中的http模块可以创建编写服务器 引入 const http = require('http') http举例使用: const http = require('http') //引入 ...
- 【笔记】机器学习 - 李宏毅 - 10 - Tips for Training DNN
神经网络的表现 在Training Set上表现不好 ----> 可能陷入局部最优 在Testing Set上表现不好 -----> Overfitting 过拟合 虽然在机器学习中,很容 ...
- TTradmin v2.1 【2019年12月12日更新】简单好用的临时远程协助软件
TTradmin 是一款免端口映射可直接穿透任何内网,基于VNC核心的即时远程协助软件.在使用的时候只需要保证“协助端”和“被协助端”使用同一个验证码即可实现安全便捷的远程控制,不需要进入路由 ...
- while语句的使用
学习while语句的正确用法:题目:老师教学生,讲一次之后 问学生会不会,如果不会:就再讲一遍.如果会了就放学,但是如果连讲了十遍还是不会,那也要放学回家 namespace _44讲十遍会不会 { ...
- vsftp安装及配置
一.安装Vsftp 1,安装 yum install vsftpd -y
- sudo的简单用法
su: Switch User, 以管理员身份运行某些命令: su -l root -c 'COMMAND' 但是想要限制某个用户只拥有一部分管理员权限,而不是拥有全部权限,这就需要用到sudo su ...
- JavaDay7(下)
问题1描述 编写一个方法,返回一个double类型二维数组,数组中的元素通过解析字符串参数获得. 代码实现: public class ArrayParser { public static void ...
- Give me five !
@media only screen and (max-width: 360px) { #friedsGroup { grid-template-columns:1fr!important; } } ...
- PHP 冷知识
1,执行Linux命令 <?php $a =`ls -a /`; // execute linux command echo '<pre>'.$a; 2.为变量起别名 <?ph ...
- linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦(三)
linux centos7环境下安装apache2.4+php5.6+mysql5.6 安装及踩坑集锦(三) 安装PHP 1.yum方式安装PHP方法同安装apache一样传送门:linux cent ...