Leetcode 211. Add and Search Word - Data structure design
2016-06-22 09:27
543 查看
211. Add and Search Word - Data structure design
Total Accepted: 26865 TotalSubmissions: 132696 Difficulty: Medium
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-zor
..
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.
click to show hint.
You should be familiar with how a Trie works. If not, please work on this problem: Implement
Trie (Prefix Tree) first.
在下午刚写完208之后,默默写了这个题。
第一次没有任何check submit直接AC,感觉很爽。
思路跟208差不多,就是多了个.万能适配符,碰到则遍历所有child节点,有满足条件的return true就行了。
public class WordDictionary { // 22ms TrieNode root; public WordDictionary(){ this.root = new TrieNode(); } // Adds a word into the data structure. public void addWord(String word) { if(word.length()==0) return; char[] words = word.toCharArray(); root.insert(words, 0); } // Returns if the word is in the data structure. A word could // contain the dot character '.' to represent any one letter. public boolean search(String word) { if(word.length()==0) return true; char[] words = word.toCharArray(); return root.search(words, 0); } class TrieNode{ char label; TrieNode[] child; boolean isWord; public TrieNode(){ this.label = 'R'; this.child = new TrieNode[26]; this.isWord = false; } public TrieNode(char label){ this.label = label; this.child = new TrieNode[26]; this.isWord = false; } public void insert(char[] words, int cur){ int pos = words[cur] - 'a'; if(child[pos] == null) child[pos] = new TrieNode(words[cur]); if(cur == words.length-1){ child[pos].isWord = true; return; } child[pos].insert(words, cur + 1); } public boolean search(char[] words, int cur){ if(cur == words.length-1){ if(words[cur] == '.'){ for(int i = 0; i < 26; i++){ if(child[i] != null && child[i].isWord == true) return true; } return false; }else{ int pos = words[cur] - 'a'; if(child[pos] != null && child[pos].isWord == true) return true; return false; } } if(words[cur] == '.'){ for(int i = 0; i < 26; i++){ if(child[i] == null) continue; if(child[i].search(words, cur + 1)) return true; } return false; }else{ int pos = words[cur] - 'a'; if(child[pos] == null) return false; return child[pos].search(words, cur + 1); } } } } // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
相关文章推荐
- 识设计大师(Logo Design Studio) v3.1.0.0 零售版 下载
- 基于Bootstrap实现Material Design风格表单插件 附源码下载
- Python Trie树实现字典排序
- Java中实现双数组Trie树实例
- 【MaterialEditText】 Material Design 的 EditText 3ff8
- 开源硬件的价值评估
- 基于Bootstrap实现Material Design风格表单插件 附源码下载
- 《Design patterns》读书笔记
- Eclipse中批量改变文件的默认打开方式
- CPU显卡内存与3DMAX渲染的关系
- Design Pattern 新解
- 39_03_Linux集群系列之十三——高可用集群之corosync基础概念及安装配置(笔记)
- 20个漂亮的单页传单设计欣赏
- 转 C ++书单
- 单例分享(循环引用及内存占用解决)
- 。「设计营商周」每年均吸引来自设计、商界、中小企及教育界等全球优秀的设计精英汇聚香港
- 「设计营商周论坛」,非常荣幸邀请到多位国际知名大师级讲者出席
- 五种常见的php设计模式
- 26个复古风格网站设计欣赏
- 命令模式--设计模式(一)