LeetCode 211. 添加与搜索单词 - 数据结构设计

设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)

search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

目前调试时间过长

class WordDictionary {
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        vector<string>  Dic ;
    }
       vector<string>  Dic ;
    /** Adds a word into the data structure. */
    void addWord(string word) {
        Dic.push_back(word) ;
       
    }
   
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
       
        bool flag = false ;
        for(int i = 0 ;i<Dic.size();i++)
        {
            for(int j = 0;j< word.size();j++)
            {
                if(Dic[i].size() != word.size())
                {
                   
                    break;
                }
               
                if(word[j]=='.'||word[j]==Dic[i][j])
                {
                    flag = true ;
                }
                else
                {
                    flag = false ;
                   
                }
                if(flag == false)
                {
                    break ;
                }  
            }
                if(flag == true)
                {
                    break ;
                }
           
        }
        if(flag)
        {
         return true ;          
        }
        else
        {
        return false ;           
        }
    }
};
/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * bool param_2 = obj.search(word);
 */

猜你喜欢

转载自blog.csdn.net/wx734518351/article/details/80246227
今日推荐