LeetCode 实现 Trie (前缀树)
题目链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree/
题目大意:
略。
分析:
代码如下:
class Trie {
public:
int passed; // 记录经过这个节点的字符串数量
int ends; // 记录有多少个字符串以这个节点结尾
unordered_map< char, Trie* > nxt;
/** Initialize your data structure here. */
Trie() {
passed = ;
ends = ;
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* p = this;
for(int i = ; i < word.size(); ++i) {
if(p->nxt.find(word[i]) == p->nxt.end()) {
p->nxt[word[i]] = new Trie();
}
++p->passed;
p = p->nxt[word[i]];
}
++p->ends;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* p = this;
for(int i = ; i < word.size(); ++i) {
if(p->nxt.find(word[i]) == p->nxt.end()) return false;
p = p->nxt[word[i]];
}
return p->ends != ;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Trie* p = this;
for(int i = ; i < prefix.size(); ++i) {
if(p->nxt.find(prefix[i]) == p->nxt.end()) return false;
p = p->nxt[prefix[i]];
}
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);
*/
LeetCode 实现 Trie (前缀树)的更多相关文章
- 第15个算法-实现 Trie (前缀树)(LeetCode)
解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 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 ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
- Leetcode 208.实现前缀树
实现前缀树 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert ...
- 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
随机推荐
- 使用requests_html抓取数据
from requests_html import HTMLSession import json class YejiCollege: def __init__(self, url): self.u ...
- [SOL] #148. 数字格子问题
说实话这题确实挺菜的... 废话少说,直接上代码^O^ Code: #include <bits/stdc++.h> using namespace std; inline int rea ...
- daily plan -- 2019/5/20
1.课内作业:物联网工程导论论文. 2.实验项目计划:学习Kinect彩色帧读取. 3.算法:LeetCode 动态规划一题. 4.英语:听力30分钟训练,英语单词. 今日心情: 进度反馈:计划基本完 ...
- 9、numpy——数组操作
Numpy 中包含了一些函数用于处理数组,大概可分为以下几类: (1)修改数组形状 (2)翻转数组 (3)修改数组维度 (4)连接数组 (5)分割数组 (6)数组元素的添加与删除 1.修改数组形状 函 ...
- UVAlive 6756 Increasing Shortest Path
We all love short and direct problems, it is easier to write, read and understand the problem statem ...
- Little Sub and Mr.Potato's Math Problem (构造法)
题目传送门Little Sub and Mr.Potato's Math Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Littl ...
- supermap idesktop连接oraclesptial数据源
1.要使用相同的版本,如iServer 9D, iDesktop9D ,32位的 plsql,32位的 oracleinstance_client 11g 2.当时遇到的问题是使用oracleinst ...
- Css3-颜色 color
一.颜色 RGBA RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法:color:rgb ...
- docker-compose启动报错:Creating network "soft_default" with the default driver ERROR: cannot create network e5b60fc347db868e471b61ea185fd24e3ea7e2730149d91ad70baf29732aaff0 (br-e5b60fc347db): conflicts wi
docker-compose启动容器时出现报错 Creating network "soft_default" with the default driver ERROR: can ...
- 140-基于双TI DSP TMS320C6670+XC7K480T的6UCPCI Express高速数据处理平台
基于双TI DSP TMS320C6670+XC7K480T的6UCPCI Express高速数据处理平台 一.板卡概述: 本技术开发主要是支持客户完成基于TI DSP TMS320C6678芯片和X ...