当前位置:网站首页>208. implement trie (prefix tree)
208. implement trie (prefix tree)
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
208. Realization Trie ( Prefix tree )
Original title link :https://leetcode.cn/problems/implement-trie-prefix-tree/
Trie( It sounds like “try”) Or say Prefix tree It's a tree data structure , Keys for efficient storage and retrieval of string datasets . This data structure has quite a number of application scenarios , For example, automatic completion and spelling check .
Please realize Trie class :
Trie() Initialize the prefix tree object .
void insert(String word) Insert a string into the forward tree word .
boolean search(String word) If the string word In the prefix tree , return true( namely , Has been inserted before retrieval ); otherwise , return false .
boolean startsWith(String prefix) If a string has been inserted before word One of the prefixes of is prefix , return true ; otherwise , return false .
Example :
Input
[“Trie”, “insert”, “search”, “search”, “startsWith”, “insert”, “search”]
[[], [“apple”], [“apple”], [“app”], [“app”], [“app”], [“app”]]
Output
[null, null, true, false, true, null, true]
explain
Trie trie = new Trie();
trie.insert(“apple”);
trie.search(“apple”); // return True
trie.search(“app”); // return False
trie.startsWith(“app”); // return True
trie.insert(“app”);
trie.search(“app”); // return True
Tips :
1 <= word.length, prefix.length <= 2000
word and prefix It's only made up of lowercase letters
insert、search and startsWith Call the number A total of No more than 3 * 104 Time
Their thinking :
Build the node of the tree , Use dictionary to store subtree structure ,{word_char: subtree }, Identify the path from the root node to the current node , Constitute a keyword .
Code implementation :
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
cur_node = self.root
for c in word:
cur_node = cur_node.children[c]
cur_node.flag = True
def search(self, word: str) -> bool:
cur_node = self.root
for c in word:
cur_node = cur_node.children.get(c)
if cur_node == None:
return False
return cur_node.flag
def startsWith(self, prefix: str) -> bool:
cur_node = self.root
for c in prefix:
cur_node = cur_node.children.get(c)
if cur_node == None:
return False
return True
# Define a node
class Node(object):
def __init__(self):
# Use dictionary to store subtree structure ,{word_char: subtree }
self.children = collections.defaultdict(Node)
# Identify the path from the root node to the current node , Constitute a keyword
self.flag = False
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
边栏推荐
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- 静态库使用MFC和共享库使用MFC的区别
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
- BluePrism注册下载并安装-RPA第一章
- Ultimate dolls 2.0 | encapsulation of cloud native delivery
- The difference between MFC for static libraries and MFC for shared libraries
- 在 C 中声明函数之前调用函数会发生什么?
- 整合阿里云短信的问题:无法从静态上下文中引用非静态方法
- 【快捷键】
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
猜你喜欢
Unexpected token o in JSON at position 1 ,JSON解析问题
AfxMessageBox和MessageBox的用法
Online public network security case nanny level tutorial [reaching out for Party welfare]
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
二叉树神级遍历:Morris遍历
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
4、【WebGIS实战】软件操作篇——数据导入及处理
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
用小程序的技术优势发展产业互联网
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
随机推荐
Include() of array
241. 为运算表达式设计优先级
6. Z 字形变换
Appium fundamentals of automated testing - basic principles of appium
The combination of applet container technology and IOT
Pyramid Scene Parsing Network【PSPNet】论文阅读
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
详解Spark运行模式(local+standalone+yarn)
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
jeecgboot输出日志,@Slf4j的使用方法
Jeecgboot output log, how to use @slf4j
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
208. 实现 Trie (前缀树)
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
Leetcode 128 longest continuous sequence (hash set)
[nine day training] content III of the problem solution of leetcode question brushing Report
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
Database DDL (data definition language) knowledge points