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(" ...
随机推荐
- 利用Spring实现Hello World
因为最近要做Java Web方面的开发,所以,就像使用Spring来实现一个以前学其他程序时首先做的一个示例"Hello World!"练练手,之前用很多中语言实现过hello w ...
- Laya2.0的转变
之前一直用Laya1.x+TypeScript了,最近项目开始使用Laya2.0+AS3了 总结一下需要注意的一些事项,算是2种开发模式的区别与过渡吧 1.AS类的访问标识 必须是public,不写会 ...
- ADC(简易的DMA传输)的认识
ADC(简易的DMA传输)的认识 首先看到是ADC的特性 1.ADC的12位分辨率.不能直接测量负电压,然后是最小量程化单位是LSB=Vref+/212 2.单次和转换模式的使用 3. 从通道0到通道 ...
- 华为Android手机打开Log
华为Android手机打开Log, 显示日志方法 今天在华为u8650上调试应用程序时,发现Eclipse的log始终无法显示,在网上找了好多资料,甚至stack overflow也查了,最后终于找到 ...
- C#split的使用方式
一,在msdn中我们能看到一下几种使用 二,我们先看看经常使用的, 我们先定义一个数组 string test = "1,2,,3,4,5,6,7"; 第一种,结果大家都熟悉,就不 ...
- Django组件——用户认证
用户认证 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 .authentica ...
- [Linux]Centos7/Centos6更改系统语言
Centos7系统语言配置信息保存在/etc/locale.conf文件内 更改步骤如下: 1.使用vim打开locale.conf文件 vim /etc/locale.conf2.编辑locale. ...
- PoisonTap - 在锁屏电脑上窃取cookies,获得电脑路由,安装网络后门的工具
工具地址:https://samy.pl/poisontap/ 当PoisonTap(由Raspberry Pi Zero & Node.js开发)接入到一个锁屏或者用密码保护的电脑上时,它可 ...
- javascript 浏览器定位
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Redis Key过期事件
解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态,如果未支付,则进行处理但是key过期了redis有通知吗?答案是肯定 ...