力扣208. 实现 Trie (前缀树)
以下是我的代码,就是简单的字符串操作,可以ac但背离了题意,我之前没接触过Trie
1 class Trie:
2
3 def __init__(self):
4 """
5 Initialize your data structure here.
6 """
7 self.trie = set()
8
9 def insert(self, word: str) -> None:
10 """
11 Inserts a word into the trie.
12 """
13 self.trie.add(word)
14
15 def search(self, word: str) -> bool:
16 """
17 Returns if the word is in the trie.
18 """
19 return word in self.trie
20
21 def startsWith(self, prefix: str) -> bool:
22 """
23 Returns if there is any word in the trie that starts with the given prefix.
24 """
25 lens = len(prefix)
26 for s in self.trie:
27 if s[:lens] == prefix:
28 return True
29 return False
30
31
32 # Your Trie object will be instantiated and called as such:
33 # obj = Trie()
34 # obj.insert(word)
35 # param_2 = obj.search(word)
36 # param_3 = obj.startsWith(prefix)
以下是大佬写的,答案链接
1 class Trie {
2 private:
3 bool isEnd;
4 Trie* next[26];
5 public:
6 Trie() {
7 isEnd = false;
8 memset(next, 0, sizeof(next));
9 }
10
11 void insert(string word) {
12 Trie* node = this;
13 for (char c : word) {
14 if (node->next[c-'a'] == NULL) {
15 node->next[c-'a'] = new Trie();
16 }
17 node = node->next[c-'a'];
18 }
19 node->isEnd = true;
20 }
21
22 bool search(string word) {
23 Trie* node = this;
24 for (char c : word) {
25 node = node->next[c - 'a'];
26 if (node == NULL) {
27 return false;
28 }
29 }
30 return node->isEnd;
31 }
32
33 bool startsWith(string prefix) {
34 Trie* node = this;
35 for (char c : prefix) {
36 node = node->next[c-'a'];
37 if (node == NULL) {
38 return false;
39 }
40 }
41 return true;
42 }
43 };
力扣208. 实现 Trie (前缀树)的更多相关文章
- 力扣 - 208. 实现Trie(前缀树)
目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...
- 力扣208——实现 Trie (前缀树)
这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...
- 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 ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 4.14——208. 实现 Trie (前缀树)
前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...
- 208. 实现 Trie (前缀树)
主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 第15个算法-实现 Trie (前缀树)(LeetCode)
解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...
随机推荐
- 2020-2021 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules) C. Berpizza (STL)
题意:酒吧里有两个服务员,每个人每次都只能服务一名客人,服务员2按照客人进酒吧的顺序服务,服务员3按照客人的钱来服务,询问\(q\),\(1\)表示有客人进入酒吧,带着\(m\)块钱,\(2\)表示询 ...
- Cyclic Nacklace HDU - 3746
CC这个月底总是很郁闷,昨天他查了他的信用卡,没有任何意外,只剩下99.9元了.他很苦恼,想着如何度过这最后的几天.受"HDU CakeMan"企业家精神的启发,他想卖一些小东西来 ...
- Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) C. Bouncing Ball (后缀和,枚举)
题意:有一长度为\(n\)的平台,平台有的位置有木桩,可以使小球弹起来,小球必须从第\(p\)个位置开始,而且每次都会向右弹\(k\)个单位,然后有的位置是没有木桩的,你可以在这些的空的位置放一个木桩 ...
- JavaScript——浏览器检查
遍历opera中的所有方法
- CQRS Event Sourcing介绍
什么是CQRS模式? CQRS是Command and Query Responsibility Segregation的缩写,直译就是命令与查询责任分离的意思. 命令会改变对象的状态,但不返回任何数 ...
- Bootstrap导航组件
Bootstrap 中的导航组件都依赖同一个 .nav 类,状态类也是共用的.改变修饰类可以改变样式. 标签页 注意 .nav-tabs 类依赖 .nav 基类 <ul class=" ...
- μC/OS-III---I笔记13---中断管理
中断管理先看一下最常用的临界段进入的函数:进入临界段 OS_CRITICAL_ENTER() 退出临界段OS_CRITICAL_EXIT()他们两个的宏是这样的. 在使能中断延迟提交时: #if OS ...
- 力扣566. 重塑矩阵-C语言实现-简单题
题目 传送门 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要 ...
- webpack 5 模块联合
webpack 5 模块联合 webpack 5 https://webpack.docschina.org/concepts/module-federation/ https://github.co ...
- Iterators & Generators in depth
Iterators & Generators in depth https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/It ...