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"));    }}
原创粉丝点击