Add and Search Word - Data structure design
来源:互联网 发布:mac系统翻墙工具 编辑:程序博客网 时间:2024/05/16 06: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”) -> false
search(“bad”) -> true
search(“.ad”) -> true
search(“b..”) -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
这道题目考察的还是Trie Tree的问题,所以如果做了Implement Trie (Prefix Tree)【详情可以参考:http://blog.csdn.net/zuoyexingchennn/article/details/46315595】这道题目,这道题应该不难,唯一不同的地方就是search时候有些不同,这里,只要细心一些,应该就没问题。
AP程序如下:
【注】:这里的类名和leetcode原题的类名略有不同!
public class AddAndSearchWordDataStructureDesign { private TrieNode root; public AddAndSearchWordDataStructureDesign () { root = new TrieNode(); } public AddAndSearchWordDataStructureDesign(TrieNode root){ this.root=root; } // Adds a word into the data structure. public void addWord(String word) { TrieNode cur=root; for(int i=0;i<word.length();i++){ int index=word.charAt(i)-'a'; if(cur.children[index]==null){ cur.children[index]=new TrieNode(word.charAt(i)); } cur=cur.children[index]; } cur.wordEnd=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) { TrieNode cur=root; for(int i=0;i<word.length();i++){ if(word.charAt(i)=='.'){ if(i==word.length()-1){ for(int j=0;j<26;j++){ if(cur.children[j]!=null&&cur.children[j].wordEnd) return true; } return false; } for(int j=0;j<26;j++){ if(cur.children[j]!=null&&new AddAndSearchWordDataStructureDesign(cur.children[j]).search(word.substring(i+1))) return true; } return false; }else{ int index=word.charAt(i)-'a'; if(cur.children[index]==null){ return false; } cur=cur.children[index]; } } if(!cur.wordEnd) return false; return true; } class TrieNode{ public TrieNode[] children=new TrieNode[26];; public char c; public boolean wordEnd; public TrieNode() { } public TrieNode(char c){ this.c=c; } }}
0 0
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- Add and Search Word - Data structure design
- LeetCode 211 - Add and Search Word - Data structure design
- LeetCode Add and Search Word - Data structure design
- 211Add 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
- 开 合 肥 发 票
- 设计模式之三:装饰模式(Decorator)
- 哈 尔 滨 开 发 票
- 哈 尔 滨 发 票
- 【汇总】语料库资源
- Add and Search Word - Data structure design
- 常 州 代 开 发 票
- 开 常 州 发 票
- 软件工程师薪福指南
- 腾讯网页重构笔试总结
- 省市级联
- 常 州 开 发 票
- HTML5
- hdu 2571 命运(dp)