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

比如这个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. fatal error LNK1169: one or more multiply defined symbols found

    在 Project/Setting/Link/General中的 Project Options: 加入 /FORCE:MULTIPLE即可")可以解决报错问题,但是这些问题全部变成了war ...

  2. Visionpro学习笔记(壹)

    注册4年,第一次发了随笔.我的博客将主要涉及到visionPro软件的学习,labview数据采集方面的思考,c#及VS的学习 此随笔系列主要是关于VisionPro(以后简称VP)的学习及使用. 近 ...

  3. script标签的async和defer

    兼容性 IE对于defer一直都支持,async属性IE6-9都没有支持,IE10及以上支持 相同点与不同点 带有async或defer的script都会立刻下载并不阻塞页面解析,而且都提供一个可选的 ...

  4. openlayers轨迹播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. gulp常用插件之gulp-rev-format使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-rev-format这是一款提供静态资产的哈希格式选项(前缀,后缀,最后扩展名). 更多使用文档请点击访问gulp-rev-format ...

  6. install multiple versions of CUDA

    https://www.pugetsystems.com/labs/hpc/How-To-Install-CUDA-10-together-with-9-2-on-Ubuntu-18-04-with- ...

  7. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  8. 爬取豆瓣音乐TOP250的数据

    参考网址:https://music.douban.com/top250 因为详细页的信息更丰富,本次爬虫在详细页中进行,因此先爬取进入详细页的网址链接,进而爬取数据. 需要爬取的信息有:歌曲名.表演 ...

  9. CSS的列表样式和网页背景

    CSS的列表样式 1. 设置title和列表 HTML: <!DOCTYPE html><html lang="en"><head>    &l ...

  10. the import javax.jms cannot be resolved问题

    JDK中并没有javax.jms包,你需要一个JMS实现(比如:activemq),并确认相应的jar被包含在CLASSPATH中. http://activemq.apache.org/ 5.5.0 ...