博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】211. Add and Search Word - Data structure design
阅读量:7052 次
发布时间:2019-06-28

本文共 2544 字,大约阅读时间需要 8 分钟。

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.

For example:

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

Note:

You may assume that all words are consist of lowercase letters a-z.

You should be familiar with how a Trie works. If not, please work on this problem:   first.
 
Trie树的递归版。
class TrieNode{public:    TrieNode* children[26];    bool end;    TrieNode()    {        for(int i = 0; i < 26; i ++)            children[i] = NULL;        end = false;    }};class WordDictionary {public:    WordDictionary()    {        root = new TrieNode();    }        // Adds a word into the data structure.    void addWord(string word) {        TrieNode* cur = root;        int i = 0;        while(i < word.size() && cur->children[word[i]-'a'] != NULL)        {            cur = cur->children[word[i]-'a'];            i ++;        }        if(i == word.size())            cur->end = true;        else        {            while(i < word.size())            {                cur->children[word[i]-'a'] = new TrieNode();                cur = cur->children[word[i]-'a'];                i ++;            }            cur->end = true;        }    }    // 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) {        return search(word, root);    }        bool search(string word, TrieNode* cur)    {        if(cur == NULL)            return false;        else if(word == "")            return (cur->end == true);        else        {            if(word[0] != '.')            {                if(cur->children[word[0]-'a'] == NULL)                    return false;                else                    return search(word.substr(1), cur->children[word[0]-'a']);            }            else            {                for(int i = 0; i < 26; i ++)                {                    if(search(word.substr(1), cur->children[i]))                        return true;                }                return false;            }        }    }        TrieNode* root;};// Your WordDictionary object will be instantiated and called as such:// WordDictionary wordDictionary;// wordDictionary.addWord("word");// wordDictionary.search("pattern");

转载地址:http://hgsol.baihongyu.com/

你可能感兴趣的文章
Spring-cloud & Netflix 源码解析:Eureka 服务注册发现接口 ****
查看>>
ORACLE里锁有以下几种模式,v$locked_object,locked_mode
查看>>
【树莓派】Linux 测网速及树莓派源
查看>>
Java用户线程和守护线程
查看>>
[TypeScript] Use the never type to avoid code with dead ends using TypeScript
查看>>
Javascript 与 SPA单页Web富应用
查看>>
SpringMVC之访问静态文件
查看>>
【java设计模式】之 模板方法(Template Method)模式
查看>>
【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!...
查看>>
linux source命令的用法
查看>>
微信小程序把玩(二十六)navigator组件
查看>>
Visual Studio 2017正式版发布全纪录
查看>>
1520-win10
查看>>
thinkjs——moment.js之前后台引入问题
查看>>
Java 线程内异常处理
查看>>
二:vlan,gre,vxlan
查看>>
静态内部类和非静态内部类的区别
查看>>
C语言 · 栅格打印问题
查看>>
【mysql】备份篇2:使用java程序定期备份mysql数据库
查看>>
BZOJ 4514: [Sdoi2016]数字配对 [费用流 数论]
查看>>