leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert
, search
, 和 startsWith
这三个操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:
- 你可以假设所有的输入都是由小写字母
a-z
构成的。 - 保证所有输入均为非空字符串。
思路分析
模板题,就没有什么好说的了,直接上代码。
#include<bits/stdc++.h>
using namespace std;
const int nch = 26;
const int maxn = 200010;
static auto x = [](){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
return 0;
}();
struct Node {
int ch[nch];
bool flag;
void reset(){
for(int i = 0; i < nch; ++i){
ch[i] = -1;
}
flag = false;
}
};
Node tree[maxn];
class Trie {
public:
/** Initialize your data structure here. */
int tot;
Trie() {
tree[tot = 0].reset();
}
/** Inserts a word into the trie. */
void insert(string word) {
int root = 0;
for(auto &chr:word) {
if(tree[root].ch[chr - 'a'] == -1) {
tree[root].ch[chr - 'a'] = ++tot;
tree[tot].reset();
}
root = tree[root].ch[chr - 'a'];
}
tree[root].flag = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
int root = 0;
for(auto &chr: word) {
if(tree[root].ch[chr - 'a'] == -1)
return false;
root = tree[root].ch[chr - 'a'];
}
return tree[root].flag;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int root = 0;
for(auto &chr: prefix) {
if(tree[root].ch[chr - 'a'] == -1)
return false;
root = tree[root].ch[chr - 'a'];
}
return true;
}
};
int main() {
return 0;
}
leetcode 208. 实现 Trie (前缀树)的更多相关文章
- Java实现 LeetCode 208 实现 Trie (前缀树)
208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...
- [leetcode] 208. 实现 Trie (前缀树)(Java)
208. 实现 Trie (前缀树) 实现Trie树,网上教程一大堆,没啥可说的 public class Trie { private class Node { private int dumpli ...
- 力扣 - 208. 实现Trie(前缀树)
目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...
- 力扣208——实现 Trie (前缀树)
这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...
- 4.14——208. 实现 Trie (前缀树)
前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...
- 208. 实现 Trie (前缀树)
主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...
- 力扣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 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
随机推荐
- 分类算法简介 基于R
最近的关键字:分类算法,outlier detection, machine learning 简介: 此文将 k-means,decision tree,random forest,SVM(supp ...
- 【洛谷P1090】合并果子
合并果子 题目链接 贪心:每次先合并最小的两堆果子 用堆实现 #include<iostream> #include<cstdio> using namespace std; ...
- 【Java-Method】读《重构》有感_Java方法到底是传值调用还是传引用调用(传钥匙调用)
今天读<重构>P279, Separate Query from Modifier,将查询函数和修改函数分离. 问题的产生 突然想到 Java 的传对象作为参数的方法到底是 传引用调用,还 ...
- 遗忘的html标签
<span>x</span><sup>2</sup><span>+y=10</span> <br> <span ...
- 17、SpringBoot------整合dubbo
SpringBoot整合Dubbo+Zookeaper 1.安装运行zookeeper (1)下载zookeeper 官网:http://zookeeper.apache.org/ (2)解压缩 (3 ...
- JavaWeb-拦截器,过滤器,监听器的区别和执行顺序
一:拦截器 :是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,springmvc的aop中的前置通知和后置通知. 二: ...
- JS底层挖掘
//Promise版本的Ajaxconst getJSON = function(url) { const promise =new Promise(function(resolve, reject) ...
- tomcat8080端口占用解决办法
打开控制台,在窗口中输入指令:netstat -ano | findstr 8080 指令的意思是找出占用8080端口的进程pid 上图中表示占用进程pid为23288,然后再次输入指令: ...
- centos 7 编译安装mysql 详细过程
一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...
- PAT (Basic Level) Practice 1040 有几个PAT
个人练习 字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位( ...