【leetcode】208. Implement Trie (Prefix Tree 字典树)
- Trie() Initializes the trie object.
- void insert(String word) Inserts the string word into the trie.
- boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
- boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, andfalse otherwis
struct TreeNode {
VALUETYPE value; //结点值
TreeNode* children[NUM]; //指向孩子结点 //指针数组
};
/**
* Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Trie {
bool isEnd; //到当前节点是否结束
Trie* next[26]; //指向孩子结点 //指针数组
};
按照上述字典树的介绍,先构建字典树,每一级用一个长度为26的指针数组来存储当前的字符以及下级的位置。
class Trie {
private:
bool isEnd;
Trie *next[26];
public:
Trie() {
isEnd=false;
memset(next,0,sizeof(next));//初始化多叉树的索引
}
void insert(string word) {
Trie *node=this;
for(auto ww:word){
if(node->next[ww-'a']==NULL){
node->next[ww-'a']=new Trie();
}
node=node->next[ww-'a'];
}
node->isEnd=true;
}
bool search(string word) {
Trie *node=this;
for(auto ww:word){
if(node->next[ww-'a']==NULL) return false;
node=node->next[ww-'a'];
}
return node->isEnd;
}
bool startsWith(string prefix) {
Trie *node=this;
for(auto ww:prefix){
if(node->next[ww-'a']==NULL) return false;
node=node->next[ww-'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);
*/
【leetcode】208. Implement Trie (Prefix Tree 字典树)的更多相关文章
- 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 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 ...
- 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 ...
随机推荐
- 我的笔记本电脑瞬间扩大一个T的容量!
前言 不知道有多少人在家里搭建中央存储设备的,也就是NAS.这个东西在我日常生活中,存储了大量的个人资料,家人们的照片,技术的资料,还有各种高清影视剧.搭配公网的IP,可以真正做到,任何时候任何地点的 ...
- Typecho部署小破站
写在前面 以前利用 Github Page + Hexo框架 + Next主题搭建过静态博客,没错就是那个黑白色系的网页!但是体验并不是很好,一来本身是静态网页,页面内容要修改都需要在本地修改完上传到 ...
- C语言图书管理借阅系统——ncurses库的使用
一.前言 作为一只大四狗,最近还跟着大二同学修了一门课(当然不是之前没通过啦),课程是高级语言课程设计,高级语言指的是C语言 :),内容是做一个XX管理系统,我选择了图书管理系统,先介绍下我做的系统: ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- Bootstrap-2栅格系统
栅格系统(使用最新版本bootstrap) Grid options(网格配置) Responsive classes(响应式class) Gutters(间距) Alignment(对齐方式) Re ...
- 经过4次优化我把python代码耗时减少95%
背景交代 团队做大学英语四六级考试相关服务.业务中有一个care服务,购买了care服务考试不过可以全额退款,不过有一个前提是要完成care服务的任务,比如坚持背单词N天,完成指定的试卷. 在这个背景 ...
- M1配置php环境完整版(用于M1芯片的Mac中,php开发环境,比如wordpress、"或wp"、emlog pro、typecho等本地开发环境的配置)
因为macbook发布的M1是基于arm架构的,导致很多软件在短时间没无法兼容,其中包括php的很多集成开发环境软件.于是需要手动配置.网上的信息也是零七八碎,故制作了这个完整的教程. 本教程基于的m ...
- Python 爬取妹子图(技术是无罪的)
... import requests from bs4 import BeautifulSoup import os import sys class mzitu(): def html(self, ...
- OWASP-Top5-(Security Misconfiguration 安全配置错误)
概述 从上一版的第 6 位开始,90% 的应用程序都经过了某种形式的错误配置测试.随着更多转向高度可配置的软件,看到这一类别上升也就不足为奇了.值得注意的CWE包括CWE-16 Configurati ...
- [bzoj4651]网格
考虑将最上中最左的跳蚤孤立,容易发现他的上面和左面没有跳蚤,因此只需要将他的右边和下边替换掉就可以了答案为-1有两种情况:1.c>=n*m-1;2.c=n*m-2且这两只跳蚤相邻对于其他情况,将 ...