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


题意:创建一个单词库,支持两种操作,给库里面加入新单词和查询某单词是否在库中,但是注意用’.’来做通配符,可以代替任意字符.
分析:trie树的改造.主要是通配符的处理.这里把搜索函数处理了一下,用递归实现,当配件通配符号的时候遍历它的所有26个分枝来求满足条件的.

code:


class WordDictionary {
private:
    struct node {
        int have;
        node *next[26];

        node(void) {
            have = 0;
            for (int i = 0; i < 26; i++) next[i] = NULL;
        }
    };

public:

    node *root = new node;

    // Adds a word into the data structure.
    // The time consumer is O(n)
    void addWord(string word) {
        node *p = root;
        for (int i = 0; i < word.size(); i++) {
            int aim = word[i] - 'a';
            if ((p -> next)[aim] == NULL) (p -> next)[aim] = new node;
            p = (p -> next)[aim];
        }
        (p -> have) = (p -> have) + 1;
    }

    bool can(node *p, int head, const string &word) {
        if (head == word.size() && p -> have) return true;
        int aim = word[head] == '.' ? 26 : word[head] - 'a';
        if (aim == 26) {
            for (int i = 0; i < 26; i++)
                if ((p -> next)[i] != NULL && can( (p -> next)[i], head + 1, word)) return true;
            return false;
        }
        if ((p -> next)[aim] != NULL)
            return can((p -> next)[aim], head + 1, word);
        return false;
    }

    // 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) {
        node *p = root;
        int head = 0;
        return can(p, head, word);
    }
};

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值