Leetcode211. Add and Search Word
来源:互联网 发布:如何导入数据到excel 编辑:程序博客网 时间:2024/05/16 06:53
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 WordDictionaryNode { public WordDictionaryNode[] children; public char data; public int freq; public WordDictionaryNode() { children = new WordDictionaryNode[26]; freq = 0; }}public class WordDictionary{ private WordDictionaryNode root; public WordDictionary(){ root=new WordDictionaryNode(); } public void addWord(String word){ if(word.length()==0){ return; } addWord(root,word.toLowerCase().toCharArray(),0); } private void addWord(WordDictionaryNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k<0||k>25){ throw new RuntimeException("charArray[index] is not a alphabet!"); } if(rootNode.children[k]==null){ rootNode.children[k]=new WordDictionaryNode(); rootNode.children[k].data=charArray[index]; } if(index==charArray.length-1){ rootNode.children[k].freq++; return; }else{ addWord(rootNode.children[k],charArray,index+1); } } public boolean search(String word){ if(word.length()==0){ return false; } return search(root, word.toCharArray(), 0); } public boolean search(WordDictionaryNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k!=-51&&(k<0||k>25)){ throw new RuntimeException("charArray[index] is not a alphabet!"); } if (k==-51){ if (index == charArray.length - 1) { for (WordDictionaryNode tmp:rootNode.children){ if (tmp!=null&&tmp.freq>0) return true; } return false; } boolean flag = false; for (WordDictionaryNode tmp:rootNode.children) { if (tmp!=null) { flag = search(tmp, charArray, index + 1); if (flag) break; } } return flag; } else { if (rootNode.children[k] == null) { return false; } if (index == charArray.length - 1) { if (rootNode.children[k].freq>0) return true; else return false; } return search(rootNode.children[k], charArray, index + 1); } }}//构造点class WordDictionaryNode { public WordDictionaryNode[] children; public char data; public int freq;//记录出现的频率 public WordDictionaryNode() { children = new WordDictionaryNode[26];//因为题目中说都是小写 freq = 0; }}public class WordDictionary{ private WordDictionaryNode root; public WordDictionary(){ root=new WordDictionaryNode(); } public void addWord(String word){ if(word.length()==0){ return; } addWord(root,word.toLowerCase().toCharArray(),0); } private void addWord(WordDictionaryNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k<0||k>25){ throw new RuntimeException("charArray[index] is not a alphabet!"); } if(rootNode.children[k]==null){ rootNode.children[k]=new WordDictionaryNode(); rootNode.children[k].data=charArray[index]; } if(index==charArray.length-1){ rootNode.children[k].freq++; return; }else{ addWord(rootNode.children[k],charArray,index+1); } } public boolean search(String word){ if(word.length()==0){ return false; } return search(root, word.toCharArray(), 0); } public boolean search(WordDictionaryNode rootNode,char[]charArray,int index){ int k=charArray[index]-'a'; if(k!=-51&&(k<0||k>25)){//.是-51 throw new RuntimeException("charArray[index] is not a alphabet!"); } if (k==-51){ if (index == charArray.length - 1) { for (WordDictionaryNode tmp:rootNode.children){ if (tmp!=null&&tmp.freq>0) return true;//freq>0来保证查找的单词不是之前输入的子串 } return false; } boolean flag = false; for (WordDictionaryNode tmp:rootNode.children) {//当是"."的时候就把这一层都加入 if (tmp!=null) { flag = search(tmp, charArray, index + 1); if (flag) break; } } return flag; } else { if (rootNode.children[k] == null) { return false; } if (index == charArray.length - 1) { if (rootNode.children[k].freq>0) return true; else return false; } return search(rootNode.children[k], charArray, index + 1); } } public static void main(String[] args) { WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("WordDictionary"); wordDictionary.addWord("addWord"); wordDictionary.addWord("search"); System.out.println(wordDictionary.search("addwor")); }}
阅读全文
0 0
- Leetcode211. Add and Search Word
- LeetCode211. Add and Search Word
- Leetcode211 - Add and Search Word - Data structure design
- LeetCode211:Add and Search Word - Data structure design
- LeetCode211. Add and Search Word - Data structure design前缀树(Trie,字典树)实战
- add-and-search-word
- Add and Search Word
- Add and Search Word
- Add and Search Word
- [LintCode]Add and Search Word
- 211. Add and Search Word
- 211. Add and Search Word
- 211. Add and Search Word
- [LeetCode] Add and Search Word
- leetcode--Add and Search Word
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- 跨域源资源共享CORS
- 在 Linux 中如何自动批量创建用户
- Linux -自动安装系统
- (13)xml入门三
- C++之为“异常安全”而努力是值得的(29)---《Effective C++》
- Leetcode211. Add and Search Word
- 素数检测算法
- bzoj2342 [Shoi2011]双倍回文(manacher+暴力/set)
- Zend Studio 12.5.1(64位)破解汉化教程
- Codeforces #390 (Div. 2) B. Ilya and tic-tac-toe game ( DFS
- UVA12174Shuffle
- bzoj1340: [Baltic2007]Escape逃跑问题
- 静态顺序表的实现
- bzoj2434 阿狸的打字机NOI2011ac自动机+fail树+树状数组+dfs序详解