原题

以下是我的代码,就是简单的字符串操作,可以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 (前缀树)的更多相关文章

  1. 力扣 - 208. 实现Trie(前缀树)

    目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...

  2. 力扣208——实现 Trie (前缀树)

    这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...

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

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

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

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

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

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

  6. 4.14——208. 实现 Trie (前缀树)

    前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...

  7. 208. 实现 Trie (前缀树)

    主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...

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

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

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

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

随机推荐

  1. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

  2. Codeforces Round #664 (Div. 2) D. Boboniu Chats with Du

    传送门:cf1395D 题意 给定一个长度为n的数组a[i]为当天说话的有趣值,如果a[i]>m,那么在 i 之后有d天不能说话.否则可以每天都说话.找到一个排列使得n天有趣值总和最大,问有趣值 ...

  3. Java的awt包的使用实例和Java的一些提示框

    一.awt的一些组件 Label l1=new Label("姓名:"); //标签 Label l2=new Label("密码:"); TextField ...

  4. bochs 调试 com 文件 magicbreak

    参考 https://blog.csdn.net/housansan/article/details/41833581 在网上看到2中解决此问题的方法:1.使用dos下的debug32工具单步跟踪pm ...

  5. codeforces 911D

    D. Inversion Counting time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. mark::开源绘图工具graphviz

    http://blog.csdn.net/iamljj/article/details/5862930 http://codeforces.com/contest/601/problem/D

  7. BZOJ3211 花神游历各国(分块 区间开根号)

    题意:给n个数,可以进行两种操作:给区间[l,r]每个数开方向下取整:算区间[l,r]的和. 思路:我们可以知道,一个数一直开方下去,就会变成0或者1,然后就不会变了.那么当一个区间只剩0或1时,就不 ...

  8. 【原】无脑操作:Centos 7.6 + MariaDB + Rsyslog + LogAnalyzer环境搭建

    背景: 网络安全法第三章第二十一条明确规定"采取监测.记录网络运行状态.网络安全事件的技术措施,并按照规定留存相关的网络日志不少于六个月". 为了满足合规性的要求,应当建设相应的日 ...

  9. vuepress config favicon

    vuepress config favicon .vuepress/public favicons https://vuepress.vuejs.org/guide/assets.html#publi ...

  10. nest cli bug

    nest cli bug Error: Collection "@nestjs/schematics" cannot be resolved. Error: Collection ...