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)空间复杂度:字典树远远小于哈 ...
随机推荐
- 1.1 NBU基本概念
1.1 NBU基本概念 1) Policy(备份策略) 备份策略定义一台或几台服务器的备份方法.它包括哪些服务器需要备份.备份哪些目录或文件.在什么时间备份.采用什么方式进行备份等.配置NBU主要指定 ...
- 线程 task训练
1. task类表示一个线程,最简单的task的构造方法是 ,参数是Action<t>,是一个无返回值的泛型委托. 指向要执行的函数.当调用·start()方法时,就执行子线程.执行指向的 ...
- jdk8环境变量 jdk8图解安装 java8安装
JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows xp下安装有点问题,所以在WIN7下安装 WIN7操作系统有32位和64位,分别要下载对应的J ...
- C#中 property 与 attribute的区别?
C#中 property 与 attribute的区别?答:attribute:自定义属性的基类;property :类中的属性
- fcn
上几周把fcn跑了几个模型,唉,因此测试程序,整了很久,浪费时间啊. fcn做分割,其实我是想用来做检测的,但是总是觉得这个框架是以后的趋势,所以一直想要去在这个基础上做个东西,目前训练的模型还没有测 ...
- 详解LinkedHashMap如何保证元素迭代的顺序
大多数情况下,只要不涉及线程安全问题,Map基本都可以使用HashMap,不过HashMap有一个问题,就是迭代HashMap的顺序并不是HashMap放置的顺序,也就是无序.HashMap的这一缺点 ...
- OpenLDAP部署目录服务
文档信息 目 的:搭建一套完整的OpenLDAP系统,实现账号的统一管理. 1:OpenLDAP服务端的搭建 ...
- 死锁-Java代码示例
class MyThread implements Runnable{ private Object o1 = new Object(); private Object o2 = new Object ...
- scrapy之分布式
分布式爬虫 概念:多台机器上可以执行同一个爬虫程序,实现网站数据的分布爬取. 原生的scrapy是不可以实现分布式爬虫? a) 调度器无法共享 b) 管道无法共享 工具 scrapy-redis组件: ...
- Java语言基础---两变量间的交换
使用中间变量交换两个变量的值 int a = 10 , b = 11 , m; m = a; a = b; b = m; 不使用中间变量交换两个变量的值 int a = 10; int b = 11; ...