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

Add and Search Word - Data structure design leetcode 211
  • xudli
  • xudli
  • 2015年05月19日 06:12
  • 6630

[LeetCode]Add and Search Word - Data structure design,解题报告

目录目录 思路 思路 AC代码思路Design a data structure that supports the following two operations: void addWord(...
  • zinss26914
  • zinss26914
  • 2015年05月18日 16:02
  • 4953

Leetcode: Add and Search Word - Data structure design (Java)

最近尝试完全脱离C++,转而只用java刷题。这是我用java做出的第一道树。题目在这里。说几点做题思路。 1. 暴力用Hashtable存单词,显然浪费空间,每个字母每个位次应该只存一次。暴力用R...
  • youngsend
  • youngsend
  • 2015年05月24日 19:58
  • 790

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

[题目: Design a data structure that supports the following two operations:void addWord(word) bool se...
  • u014673347
  • u014673347
  • 2015年07月27日 23:24
  • 842

LeetCode 211 - Add and Search Word - Data structure design

一、问题描述 Description:Description: Design a data structure that supports the following two opera...
  • lisong694767315
  • lisong694767315
  • 2015年05月16日 10:12
  • 3260

LeetCode 211. Add and Search Word - Data structure design

借用LeetCode 208. Implement Trie (Prefix Tree)中设计的字典树,特判查询字符为.时的情况即可。 注意: 成员变量的初始化应放在构造函数的initializi...
  • u014674776
  • u014674776
  • 2015年08月10日 10:50
  • 674

leetcode -- Add and Search Word - Data structure design -- 重点

https://leetcode.com/problems/add-and-search-word-data-structure-design/同样是关于trie.只是在search的时候要dfs b...
  • xyqzki
  • xyqzki
  • 2015年12月24日 17:34
  • 293

211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(w...
  • zhouyanz
  • zhouyanz
  • 2016年03月22日 10:45
  • 185

211 Add and Search Word - Data structure design

Trie 树 + 深搜public class WordDictionary { public TrieNode root; public WordDictionary(){ root...
  • gyn18744026959
  • gyn18744026959
  • 2016年01月04日 16:06
  • 177

leetcode #211 Add and Search Word - Data structure design

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

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