题目链接: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 (前缀树)的更多相关文章

  1. 第15个算法-实现 Trie (前缀树)(LeetCode)

    解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...

  2. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

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

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

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

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

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

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

  6. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

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

  7. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  8. Leetcode 208.实现前缀树

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

  9. 实现 Trie (前缀树)

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

随机推荐

  1. Linux系统配置Java开发基本环境

    jdk安装一.用yum安装jdk1.查看yum库都有哪些jdk版本yum search java|grep jdk2.选择版本安装yum install java-1.8.0-openjdk(/usr ...

  2. dp(最长升序列:二分查找时间优化nlogn)

    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...

  3. 7、numpy——广播

    1.广播的引出 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a. ...

  4. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  5. bzoj1897. tank 坦克游戏(决策单调性分治)

    题目描述 有这样一款新的坦克游戏.在游戏中,你将操纵一辆坦克,在一个N×M的区域中完成一项任务.在此的区域中,将会有许多可攻击的目标,而你每摧毁这样的一个目标,就将获得与目标价值相等的分数.只有获得了 ...

  6. Linux磁盘分区实例演示

    一直觉得Linux的分区操作很高大上,之前在物理机上装的Linux,所以不敢随便乱来,今天重回Windows,下面利用Windows上的Linux虚拟机实际操作一次磁盘分区,要求:1G的硬盘分成2个2 ...

  7. JavaScript原型&原型链

    原型&原型对象 先来一段简单的代码: function Fun(name) { this.name = name } var obj = new Fun('obj') JavaScript中的 ...

  8. 逗号导致hive报“SemanticException Error in parsing”错误

    > select p.dt, p.cookie_qunar_global, p.refer_domain, p.kwid, p.query_word, p,traffic_type--, p.p ...

  9. Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...(转)

    对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...

  10. Vue 列表渲染中的key

    首先看一下官网的论述: 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变,Vue 将不会移动 DOM 元素来匹配数据项的顺序, 而是简单 ...