leetcode:Trie:Add and Search Word - Data structure design(211)

原创 2016年08月31日 15:03:01

https://leetcode.com/problems/add-and-search-word-data-structure-design/


class TrieNode  
{  
public:  
    bool isEnd;  
    TrieNode *children[26];  
    TrieNode() : isEnd(false)  
    {  
        for (int i = 0; i < 26; i++)  
        {  
            children[i] = NULL;  
        }  
    }  
};  

class WordDictionary {  
public:  
    WordDictionary()  
    {  
        root = new TrieNode();  
    }  

    // Adds a word into the data structure.  
    void addWord(string word) {  
        TrieNode *cur = root;  
        for (int i = 0; i < word.length(); i++)  
        {  
            int index = word[i] - 'a';  
            if (cur->children[index] == NULL)  
            {  
                cur->children[index] = new TrieNode();  
            }  

            cur = cur->children[index];  
        }  

        cur->isEnd = 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) {  
        int n = word.length();  
        return search(word, n, 0, root);  
    }  

    bool search(string &word, int n, int pos, TrieNode *cur)  
    {  
        if (cur == NULL)  
        {  
            return false;  
        }  
        if (pos == n)  
        {  
            return cur->isEnd;  
        }  

        if (word[pos] == '.')  
        {  
            for (int i = 0; i < 26; i++)  
            {  
                if (cur->children[i])  
                {  
                    if (search(word, n, pos+1, cur->children[i]))  
                    {  
                        return true;  
                    }  
                }  
            }  
        }  
        else  
        {  
            int index = word[pos] - 'a';  
            if (cur->children[index])  
            {  
                return search(word, n, pos+1, cur->children[index]);  
            }  
        }  

        return false;  
    }  
private:  
    TrieNode *root;  
};  
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 211 Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(...

Leetcode 211. Add and Search Word - Data structure design

211. Add and Search Word - Data structure design Total Accepted: 26865 Total Submissions: 132...

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

因为给定了a-z这个范围,并且字符串的添加和查找符合Trie的常用方法,因此考虑使用Trie这种数据结构。 然后和普通的Trie不同的是,要匹配正则表达式中的“.”,也就是说在这一层是无法判断沿着拿个...
  • NK_test
  • NK_test
  • 2015年09月04日 01:06
  • 1358

LeetCode 211 - Add and Search Word - Data structure design

一、问题描述 Description:Description: Design a data structure that supports the following two opera...

[leetcode] 211. Add and Search Word - Data structure design 解题报告

题目链接: https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data struc...

[Leetcode 211, Medium] Add and Search Word - Data structure design

[Leetcode 211, Medium] Add and Search Word - Data structure design

LeetCode 211. Add and Search Word - Data structure design(单词检索)

原题网址:https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data struct...
  • jmspan
  • jmspan
  • 2016年05月05日 01:43
  • 399

LeetCode 211 Add and Search Word - Data structure design

题目描述Design a data structure that supports the following two operations:void addWord(word) bool searc...

Leetcode 211 Add and Search Word - Data structure design

设计一个数据结构来支持以下两个操作:search(word)既可以查找一个字面上的直接量,也可以查找一个包含a-z和'.'的正则表达式,'.'表示任意字符。...

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

Design a data structure that supports the following two operations: void addWord(word) bool search(w...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:Trie:Add and Search Word - Data structure design(211)
举报原因:
原因补充:

(最多只允许输入30个字)