主要是记录一下这个数据结构。

比如这个trie树,包含三个单词:sea,sells,she。

代码:

 class Trie {
bool isWord;
vector<Trie*> children;
public:
/** Initialize your data structure here. */
Trie() {
isWord=false;
children.assign(,nullptr);
} /** Inserts a word into the trie. */
void insert(string word) {
if(word.empty()){return;}
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
cur->children[word[i]-'a']=new Trie();
}
cur=cur->children[word[i]-'a'];
}
cur->isWord=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
return false;
}
cur=cur->children[word[i]-'a'];
}
return cur->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
auto cur=this;
for(int i=;i<prefix.size();++i){
if(cur->children[prefix[i]-'a']==nullptr){
return false;
}
cur=cur->children[prefix[i]-'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);
*/

208. 实现 Trie (前缀树)的更多相关文章

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

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

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

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

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

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

  4. leetcode 208. 实现 Trie (前缀树)

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

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

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

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

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

  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. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

随机推荐

  1. 如何规范git commit提交

    相信很多人使用SVN.Git等版本控制工具时候都会觉得每次提交都要写一个注释有什么用啊?好麻烦,所以我每次都是随便写个数字就提交了,但是慢慢的我就发现了,如果项目长期维护或者修改很久之前的项目,没有一 ...

  2. Mybaits(10)N+1问题

    N+1问题 从上面的例子日志中我们可以看到所有级联都成功了,但是引发了性能问题,例如我们在查询雇员的信息和工作任务信息,此时体检表和工牌信息就是多余,我们没必要查询一次.如果想日志体现的那样,取出了所 ...

  3. 从Windows10中彻底删除【3D对象】文件夹

    Remove "3D object" folder from My Computer Windows Registry Editor Version 5.00 [-HKEY_LOC ...

  4. 修改Ubuntu的apt-get源为国内镜像源的方法

    1.原文件备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 2.编辑源列表文件 sudo vim /etc/apt/sources. ...

  5. 关于xshell和文件传输相关

    Xshell连接linux系统 下载Xshell后启动软件 不能连接,没有弹出输入账户和密码时在linux中执行 然后再次执行Xshell 连接成功 文件传输 点击Xshell的文件传输 会引导你去安 ...

  6. 预防XSs和sql注入常见分析

    SQL注入简介SQL 注入漏洞(SQL Injection)是 Web 开发中最常见的一种安全漏洞.可以用它来从数据库获取敏感信息,或者利用数据库的特性执行添加用户,导出文件等一系列恶意操作,甚至有可 ...

  7. springMVC请求访问的整个过程

    //以上个随笔(springMVC项目配置文件)为基础,详述springMVC请求的整个过程流向 web.xml                                            ...

  8. Maven的作用

    Maven的作用 1.在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差 ...

  9. arm的字节对齐问题总结(转)

    问题由来:pc的lsb总是0,因为代码至少要字对齐.cm3的指令至少是半字对齐的(16) 一.啥是字对齐?为啥要字对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访 ...

  10. 在Visual Studio中将dll以资源的形式嵌入exe中

    一.Dll的优点: 1.扩展应用程序的特性 2.简化项目管理 3.有助于节省内存 4.促进资源的共享 5.促进本地化 6.有助于解决平台间的差异 7.可用于特殊目的 有关于dll及注入相关理论资料,可 ...