力扣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 ...
随机推荐
- File类以及IO流
1.File类 package LESSON12; import java.io.File; import java.io.IOException; import java.text.SimpleDa ...
- Codeforces Round #622 (Div. 2) B. Different Rules(数学)
Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...
- entity framwork修改指定字段
1.ef修改时指修改指定字段public void ChangePassword(int userId, string password) { var user = new User() { Id = ...
- WPF Animation For SizeChanged Of UIElement
效果图 学到一个新词: Show me the money 背景 这几天查资料,看到 CodeProject 上面的一篇 Post <Advanced Custom TreeView Layou ...
- LINUX - 获取本地ip
Linux编程获取本机IP地址的几种方法 参考: https://blog.csdn.net/zhongmushu/article/details/89944990 https://www.cnblo ...
- markdown嵌入图片
这里嵌入指不会因为本地文件丢失而丢失. 参考:https://blog.csdn.net/testcs_dn/article/details/78952358 https://blog.csdn.ne ...
- PAT l2-010 排座位 【并查集】
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- IFIX 5.9 历史数据 曲线 (非SQL模式)
装完 ifix 5.9 默认是没有Hist 开头的 历史数据源的,没存,至少我装的版本是这样. 那个Historian 也没有安装包,好像还要授权,自己研究不了. 1 先把数据存本地 在你的安装包里 ...
- Leetcode(17)-电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- C语言经典面试题
[题目]零值比较--BOOL,int,float,指针变量与零值比较的if语句. (首先给个提示:题目中要求的是零值比较,而非与0进行比较,在C++里"零值"的范围可就大了,可以是 ...