实现一个 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 (前缀树)的更多相关文章

  1. Java实现 LeetCode 208 实现 Trie (前缀树)

    208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...

  2. [leetcode] 208. 实现 Trie (前缀树)(Java)

    208. 实现 Trie (前缀树) 实现Trie树,网上教程一大堆,没啥可说的 public class Trie { private class Node { private int dumpli ...

  3. 力扣 - 208. 实现Trie(前缀树)

    目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...

  4. 力扣208——实现 Trie (前缀树)

    这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...

  5. 4.14——208. 实现 Trie (前缀树)

    前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...

  6. 208. 实现 Trie (前缀树)

    主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...

  7. 力扣208. 实现 Trie (前缀树)

    原题 以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie 1 class Trie: 2 3 def __init__(self): 4 ""&qu ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

随机推荐

  1. 【转】关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题

    问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创建一个Android项目时,又会再多出一个appcompat_v7_ ...

  2. php中的脚本加速扩展opcache

    今儿在azure里装php5.5.4,发现原先php5.4.php5.3中的zend guard laoder以及php5.2中的Zend Optimizer均不能再用,一直很喜欢用的eacceler ...

  3. matlab 大块注释和取消注释的快捷键

    matlab 大块注释和取消注释的快捷键 注释:Ctrl+R 取消注释:Ctrl +T

  4. DTcms网站伪静态逻辑

    我们之前写伪静态就是web.config里面配置好.-->配置伪静态(URL重写),DTcms网站写的伪静态跟之前的不一样,他是静态页面和代码现实了分离.http://demo.dtcms.ne ...

  5. 绕不开的this

    犹豫两秒要不要整理this,从红皮书上看了半天,没搞懂哎(弱爆了) 什么是this?this是在执行上下文创建时期创建的一个执行过程中不可改变的变量.执行上下文是指js引擎会将代码执行前需要的变量th ...

  6. 4.vue引入axios同源跨域

    前言: 跨域方案有很多种,既然我们用到了Vue,那么就使用vue提供的跨域方案. 解决方案: 1.修改HttpRequestUtil.js import axios from 'axios' expo ...

  7. HJ浇花

    题目描述 HJ养了很多花(99999999999999999999999999999999999盆),并且喜欢把它们排成一排,编号0~999999999999999999999999999999999 ...

  8. >题解< 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是 11 米.我们可以把马路看成一个数轴,马路的一端在数轴 00 的位置,另一端在 LL 的位置:数轴上的每个整数点,即 0,1 ...

  9. pip安装第三方包总失败

    第一步:升级pip python -m pip install -U pip 第二布:安装想下载的第三方包 python -m pip install xx 一般来说pip安装不会失败的,失败的话就尝 ...

  10. 使用ansible安装配置zabbix客户端

    ansible角色简介: 目录名 说明 defaults 默认变量存放目录 handlers 处理程序(当发生改变时需要执行的操作) meta 角色依赖关系处理 tasks 具体执行的任务操作定义 t ...