[Leetcode]211. Add and Search Word - Data structure design @python
来源:互联网 发布:淘宝怎么购买东西 编辑:程序博客网 时间:2024/05/26 05:52
题目
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
For example:
addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(“.ad”) -> true
search(“b..”) -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
题目要求
要求建立一种数据结构,方便单词的模式匹配。模式包含字母和字符’.’,’.’可以表示任意的字符。
解题思路
此题参考了书影博客的解题思路。
采用了字典树作为此题的数据结构。
代码
class TrieNode(object): def __init__(self): self.childs = {} self.isWord = Falseclass WordDictionary(object): def __init__(self): """ initialize your data structure here. """ self.root = TrieNode() def addWord(self, word): """ Adds a word into the data structure. :type word: str :rtype: void """ node = self.root for letter in word: child = node.childs.get(letter) if child == None: child = TrieNode() node.childs[letter] = child node = child node.isWord = True def search(self, word): """ Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. :type word: str :rtype: bool """ return self.find(self.root,word) def find(self,node,word): if word == '': return node.isWord if word[0] == '.': for _,child in node.childs.items(): if self.find(child,word[1:]): return True elif word[0] in node.childs: return self.find(node.childs.get(word[0]),word[1:]) return False
0 0
- [Leetcode]211. Add and Search Word - Data structure design @python
- [leetcode] 211.Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- 211. Add and Search Word - Data structure design LeetCode
- leetcode 211. Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode-211.Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode 211 - Add and Search Word - Data structure design
- LeetCode Add and Search Word - Data structure design
- Leetcode Add and Search Word - Data structure design
- leetcode:Add and Search Word - Data structure design
- [leetcode]Add and Search Word - Data structure design
- [LeetCode] Add and Search Word - Data structure design
- leetcode #211 Add and Search Word - Data structure design
- 操作SQL构件
- Windows 7下硬盘安装Ubuntu 13.04图文教程
- Object-C--->Swift之(十一)属性观察者
- HDU-1253 胜利大逃亡 (BFS)
- 【slighttpd】基于lighttpd架构的Server项目实战(7)—http-parser
- [Leetcode]211. Add and Search Word - Data structure design @python
- jQuery1(jQuery介绍及$)
- 猿代码邀请码
- LeetCode90. Subsets II
- 操作权限构件
- 为什么直接把javascript的代码写进html就行但是调用js文件就不行
- uva 10859 树形dp
- HDOJ1238 Substrings
- junit4X系列--Runner解析