实现一个 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. Angular2入门--架构概览

    Angular 介绍 Angular 是一款来自谷歌的开源的web前端框架,诞生于2009年,是一款优秀的前端JS框架,已经被用于谷歌的多款产品. Angular 基于Typescript开发 ,更适 ...

  2. 2018.8.18 servlet使用的会话跟踪除session外还有哪些方式

    解释HTTP HTTP是一种无连接的协议,如果一个客户端只是单纯地请求一个文件(HTML或GIF),服务器端可以响应给客户端,并不需要知道一连串的请求是否来自于相同的客户端,而且也不需要担心客户端是否 ...

  3. vue input框设置值 一般对象都是通过打点形式取值

  4. CUDA 纹理内存

    原文链接 1.概述 纹理存储器中的数据以一维.二维或者三维数组的形式存储在显存中,可以通过缓存加速访问,并且可以声明大小比常数存储器要大的多. 在kernel中访问纹理存储器的操作称为纹理拾取(tex ...

  5. machine learning学习笔记

    看到Max Welling教授主页上有不少学习notes,收藏一下吧,其最近出版了一本书呢还,还没看过. http://www.ics.uci.edu/~welling/classnotes/clas ...

  6. ssh框架复习

    1.Hibernate中实体类的创建规则是什么? 2.hibernate中实体类的三种状态? 三种状态: 1. new 出来一个新对象 TakeTime takeTime = new TakeTime ...

  7. forEach、for...in、for...of

    forEach 数组实例的遍历方法 const arr=['red', 'green', 'blue']; arr.forEach(function(element, index) { console ...

  8. UVa中国麻将(Chinese Mahjong,Uva 11210)

    简单的回溯题 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  9. Linux关闭开启防火墙命令

    在外部访问CentOS中部署应用时,需要关闭防火墙. 关闭防火墙命令:systemctl stop firewalld.service 开启防火墙:systemctl start firewalld. ...

  10. 课时68.id选择器(掌握)

    1.什么是id选择器? 作用:根据指定的id名称找到对应的标签,然后设置属性 格式: #id名称{ 属性:值; } 注意点: 1.每个html标签都有一个属性叫做id,也就是说每个标签都可以设置id ...