[LeetCode] 211. Add and Search Word - Data structure design Java

题目:

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.

题意及分析:设计一个支持插入和查找的数据结构,插入的字符串只包含a-z的小写字母,查找的字符串还可包含‘.’(代表任何一个字母)。这道题可以其实就是要求一个字典树,每个树的节点有26个子节点,有一个布尔类型判断从根节点到该点组成的字符串是否是一个字母。查询时如果遇到‘.’就遍历查询当前节点的所有子节点,如果有一个符合便能查找到,否则查找不到。

代码:

class TrieNode{
    public char word;       //字母
    public boolean isWord;  //判断从根节点到该点是否是一个完整的单词
    public TrieNode[] childrens = new TrieNode[26];    //子节点
    public TrieNode (){}
    public TrieNode( char word0){
        TrieNode trieNode = new TrieNode();
        trieNode.word = word0;
    }
}

public class WordDictionary {

    TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
        root.word = ‘ ‘;
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode node = root;
        for(int i=0;i<word.length();){
            while(i<word.length()&&node.childrens[word.charAt(i)-‘a‘]!=null){
                node =node.childrens[word.charAt(i)-‘a‘];
                i++;
            }
            while (i<word.length()){
                node.childrens[word.charAt(i)-‘a‘] = new TrieNode(word.charAt(i));
                node = node.childrens[word.charAt(i)-‘a‘];
                i++;
            }
        }
        node.isWord = 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 node = root;
        return searchRes(0,word,node);
    }

    private boolean searchRes(int start,String word,TrieNode node) {
        if(start == word.length()){
            if(node.isWord==true) return true;
            else return false;
        }
        if(word.charAt(start)==‘.‘){        //如果某点为‘.‘
            boolean isWord = false;
            for(int i=0;i<26;i++){
                if(node.childrens[i]!=null){
                    isWord = isWord||searchRes(start+1,word,node.childrens[i]);
                }
            }
            return isWord;
        }else{
            if(node.childrens[word.charAt(start)-‘a‘]!=null){
                return searchRes(start+1,word,node.childrens[word.charAt(start)-‘a‘]);
            }else
                return false;
        }
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */
时间: 2024-10-11 17:42:31

[LeetCode] 211. Add and Search Word - Data structure design Java的相关文章

Java for LeetCode 211 Add and Search Word - Data structure design

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.

(*medium)LeetCode 211.Add and Search Word - Data structure design

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

[LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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

LeetCode 211. Add and Search Word - Data structure design(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

LeetCode 211. Add and Search Word - Data structure design

原题 设计一个包含下面两个操作的数据结构:addWord(word), search(word)addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词查询或是只包含. 和a-z的简易正则表达式的查询.一个 . 可以代表一个任何的字母. 样例 addWord("bad") addWord("dad") addWord("mad") search("pad") // return fals

【LeetCode】211. Add and Search Word - Data structure design

Add and Search Word - Data structure design 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 

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design 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 

[leetcode trie]211. Add and Search Word - Data structure design

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

[leedcode 211] Add and Search Word - Data structure design

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