LeetCode 211 Add and Search Word - Data structure design
来源:互联网 发布:微软办公软件2007 编辑:程序博客网 时间:2024/05/23 21:49
题目描述
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") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
分析
本题只能用Trie,但是……LeetCode最近加入了一个测试用例,我试了所有网上能找到的代码,都是Time Limit Exceeded!
代码
class TrieNode { boolean isLeaf; char c; HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>(); // Initialize your data structure here. public TrieNode() { } public TrieNode(char c) { this.c = c; } } public class WordDictionary { private TrieNode root; public WordDictionary() { root = new TrieNode(); } // Adds a word into the data structure. public void addWord(String word) { HashMap<Character, TrieNode> children = root.children; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); TrieNode t; if (children.containsKey(c)) { t = children.get(c); } else { t = new TrieNode(c); children.put(c, t); } children = t.children; if (i == word.length() - 1) { t.isLeaf = true; } } } // 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) { return searchNode(word, root); } private boolean searchNode(String word, TrieNode t) { if (t == null) { return false; } if (word.length() == 0) { return t.isLeaf; } HashMap<Character, TrieNode> children = t.children; char c = word.charAt(0); if (c == '.') { for (char key : children.keySet()) { if (word.length() == 1 && children.get(key).isLeaf) { return true; } if (searchNode(word.substring(1), children.get(key))) { return true; } } return false; } else if (!children.containsKey(c)) { return false; } else { return searchNode(word.substring(1), children.get(c)); } } } // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
2 0
- 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, Medium] 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-211-Add and Search Word - Data structure design
- leetcode:Trie:Add and Search Word - Data structure design(211)
- 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
- db2 外置设备建立数据库步骤
- Objc-C 知识点回顾 六 字典、集合
- 源码安装lamp
- Gosn的使用
- opencv中的矩阵操作
- LeetCode 211 Add and Search Word - Data structure design
- Objc-C 知识点回顾 七 block
- cygwin gcc 动态链接库问题
- 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
- No Dialect mapping for JDBC type: -1 问题解决
- Eclipse读取xml中文乱码问题解决
- Rotate Array
- android性能测试
- windows删除一个目录下的文件c代码