【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 ...
随机推荐
- cf12E Start of the season(构造,,,)
题意: 给一个偶数N. 构造出一个矩阵. 满足:主对角线上全为0.每一行是0~N-1的一个全排列.矩阵关于主对角线对称. 思路: 觉得是智商题,,,,看完题解后觉得不难,但是我就是没想出来.只想到了前 ...
- Github图床设置
创建新仓库 点击右上角加号->新建仓库,填写基本信息后点击下面的创建即可 https://github.com/new 创建新令牌 点击设置->开发者设置->私人令牌->生成新 ...
- Linux环境下安装、配置Redis
linux下安装redis 官网下载链接:https://redis.io/download 安装 下载redis压缩包 1.选择Stable(5.0)下的Download 5.0.0 链接进行下载 ...
- shiro550反序列化分析
拖了很久的shiro分析 漏洞概述 Apache Shiro <= 1.2.4 版本中,加密的用户信息序列化后存储在Cookie的rememberMe字段中,攻击者可以使用Shiro的AES加密 ...
- Power Platform Center of Excellence (CoE) 部署完成&主要内容说明
随着目前国内使用Power Platform的企业越来越多,而在跟客户交付项目时,客户经常想了解平台的一些基本情况: Power Platform 有多少环境,分别是谁创建和管理? Power Pla ...
- 1个月连载30个设计模式真实案例(附源码),挑战年薪60W不是梦
本文所有内容均节选自<设计模式就该这样学> 本文自2012年10月29日起持续连载,请大家持续关注.... 序言 Design Patterns: Elements of Reusable ...
- c++学习笔记1(引用)
引用 格式:类型名&引用名=某变量名: 概念 实例:编写交换整型变量的函数对比 不用引用 实机操作 使用引用 实机操作 实例2:用作函数的返回值 可对函数返回值赋值 常引用 使用格式,在引用前 ...
- Django笔记&教程 4-2 模型(models)中的Field(字段)
Django 自学笔记兼学习教程第4章第2节--模型(models)中的Field(字段) 点击查看教程总目录 参考:https://docs.djangoproject.com/en/2.2/ref ...
- [cf1305G]Kuroni and Antihype
对整个过程构造一张有向图,其中$(x,y)\in E$当且仅当$x$把$y$加入,且边权为$a_{x}$ 显然这是一棵外向树森林,并再做如下两个构造: 1.新建一个点$a_{0}=0$,将其向所有入度 ...
- [hdu6316]Odd shops
记$m=10$,即商品的种类 记$g(x)=1+\sum_{i=1}^{m}a_{i}x_{i}$,问题即求$f_{n}(x)=g^{n}(x)$非0项数(模2意义下) 注意到$f^{2}(x)\eq ...