LeetCode——Add and Search Word - Data structure design
2015-10-13 19:42
417 查看
Description:
Design a data structure that supports the following two operations:
search(word) can search a literal word or a regular expression string containing only letters
For example:
Note:
You may assume that all words are consist of lowercase letters
题目很好理解:添加单词查找单词。
首先想到的是用线性数据结构,然后逐个匹配查找。可以想象一定是超时的。
要优化明显要使用Trie树来减少无用的比较次数,从而降低时间复杂度。
关于Trie树:/article/5225757.html
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.
题目很好理解:添加单词查找单词。
首先想到的是用线性数据结构,然后逐个匹配查找。可以想象一定是超时的。
public class WordDictionary { private List<String> arr = new ArrayList<String>(); // Adds a word into the data structure. public void addWord(String word) { arr.add(word); } // 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) { boolean flag = true; for(String str : arr) { if(str.equals(word)) { flag = true; break; } else if(arr.contains(".")) { if(word.length() != str.length()) { flag = false; break; } for(int i=0; i<str.length();) { char ch1 = str.charAt(i); char ch2 = word.charAt(i); if(ch1 == '.' || ch2 == '.' || ch1 == ch2) { i ++; } else { flag = false; break; } } } else { flag = false; break; } } return flag; } } // Your WordDictionary object will be instantiated and called as such: // WordDictionary wordDictionary = new WordDictionary(); // wordDictionary.addWord("word"); // wordDictionary.search("pattern");
要优化明显要使用Trie树来减少无用的比较次数,从而降低时间复杂度。
关于Trie树:/article/5225757.html
public class WordDictionary { private TrieNode root; public WordDictionary() { this.root = new TrieNode(); } private class TrieNode { private TrieNode[] son; private char val; private boolean isEnd; public TrieNode() { this.son = new TrieNode[26]; this.isEnd = false; } } // Adds a word into the data structure. public void addWord(String word) { char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) { int pos = ch - 'a'; if(node.son[pos] == null) { node.son[pos] = new TrieNode(); node.son[pos].val = ch; } node = node.son[pos]; } node.isEnd = true; } public boolean patternSearch(String word, TrieNode node) { char[] wordChars = word.toCharArray(); for(int at=0; at<word.length(); at++) { char ch = wordChars[at]; if(ch != '.') { int pos = ch - 'a'; if(node.son[pos] != null) { node = node.son[pos]; } else { return false; } } else { int flag = 0; for(int i=0; i<26; i++) { if(node.son[i] != null) { boolean b =patternSearch(word.substring(at+1), node.son[i]); if(b) return b; else { flag ++; } } else { flag ++; continue; } } if(flag == 26) { return false; } } } return node.isEnd; } // 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 patternSearch(word, this.root); } }
相关文章推荐
- response
- PID172 / 圣诞树(RQNOJ)
- JavaWeb知识 - 分页
- MFC 基础!!
- Linux/unix疑问:-------如何从execv()函数的角度去理解 Linux上应用程序的执行机制????
- lintcode-简化路径-421
- gdb+gdbserver调试(方式1)
- OGRE针对图形API调用的效率优化
- 计算状态栏和导航条的高度
- android listview 取消头部分割线
- 软件测试基础知识总结
- 使用uitableview的索引功能
- OC学习之初步对象的建立
- Ubuntu环境下安装python的flask
- 写代码,请不要中断
- Objective-C中代码块的学习
- 侧滑返回功能失效解决
- UIView与CALayer的理解
- 有关圆的计算。
- centos7 matlab install