力扣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 ...
随机推荐
- NOIP2015提高组 信息传递 ---并查集问题
题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti 的同学. 游戏开始时,每人都只 ...
- UVA-11019 二维哈希算法
UVA-11019 题意: 就是给你AB两个字符矩阵,问你B矩阵在A矩阵中的出现次数. 题解: 参考链接:https://blog.csdn.net/qq_38891827/java/article ...
- Codeforces Round #479 (Div. 3) C. Less or Equal (排序,贪心)
题意:有一个长度为\(n\)的序列,要求在\([1,10^9]\)中找一个\(x\),使得序列中恰好\(k\)个数满足\(\le x\).如果找不到\(x\),输出\(-1\). 题解:先对这个序列排 ...
- Linux入门详解
Linux基础知识 Linux&Unix 说起Linux,就不得不提Unix操作系统. Unix系统号称世界上最稳定的系统,就连苹果公司也从中获取灵感开发出了移动端大名鼎鼎的IOS. Unix ...
- [Golang]-2 Map关联数组与下划线(_)的意义
目录 map 下划线(underscore) 用在import 用在返回值 用在变量 map map 是 Go 内置关联数据类型(在一些其他的语言中称为哈希 或者字典 ). func main() { ...
- OpenStack Train版-5.安装nova计算服务(控制节点)
nova计算服务需要在 控制节点 和 计算节点 都安装 控制节点主要安装 nova-api(nova主服务) nova-scheduler(nova调度服务) nova-conductor(n ...
- 5.Fanout交换机之新订单通知商户场景
标题 : 5.Fanout交换机之新订单通知商户场景 目录 : RabbitMQ 序号 : 5 const string newOrderQueueName = "neworder-queu ...
- codeforce 3C
B. Lorry time limit per test 2 seconds memory limit per test 64 megabytes input standard input outpu ...
- 对于kmp求next数组的理解
首先附上代码 1 void GetNext(char* p,int next[]) 2 { 3 int pLen = strlen(p); 4 next[0] = -1; 5 int k = -1; ...
- VuePress config All In One
VuePress config All In One docs/.vuepress/config.js const { title, description, } = require('../../p ...