首页 > 代码库 > 一刷leetcode——数据结构

一刷leetcode——数据结构

211. Add and Search Word - Data structure design

题意:add函数插入单词形成词典,search函数查询有没有某单词,查询时“.”可以代表任何一个字母

我的思路:字典树+递归

我的代码:数组写法

技术分享
class WordDictionary {
public:
    /** Initialize your data structure here. */
    int ch[40000][26], sz, val[40000];
    WordDictionary() {
        sz = 1;
        memset(ch[0], 0, sizeof(ch[0]));
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word) {
        int u = 0, n = word.size();
        for (int i = 0; i < n; i++) {
            int c = word[i]-a;
            if (!ch[u][c]) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = 1;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character ‘.‘ to represent any one letter. */
    bool query(int j, string word) {
        int n = word.size();
        for (int i = 0; i < n; i++) {
            if (word[i] != .) {
                j = ch[j][word[i]-a];
                if (j == 0) return 0;
            } else {
                int flag = 1, k;
                for (k = 0; k < 26; k++) {
                    if (ch[j][k]) {
                        flag = 0;
                        if (query(ch[j][k], word.substr(i+1, n-1-i))) 
                            return 1;
                    }
                }
                if (flag || k == 26) return 0;
            }
        }
        if (val[j] == 1) return 1;
        return 0;
    }
    bool search(string word) {
        return query(0, word);
    }
};
View Code

solution解法:字典树指针写法

技术分享
class TrieNode {
public:
    bool isKey;
    TrieNode* children[26];
    TrieNode(): isKey(false) {
        memset(children, NULL, sizeof(TrieNode*) * 26); 
    }
};

void addWord(string word) {
    TrieNode* run = root;
    for (char c : word) {
        if (!(run -> children[c - a]))
            run -> children[c - a] = new TrieNode();
        run = run -> children[c - a]; 
    }
    run -> isKey = true;
}

private:
    TrieNode* root;

WordDictionary() {
    root = new TrieNode();
}

class TrieNode {
public:
    bool isKey;
    TrieNode* children[26];
    TrieNode(): isKey(false) {
        memset(children, NULL, sizeof(TrieNode*) * 26);
    }
};

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

    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode* run = root;
        for (char c : word) {
            if (!(run -> children[c - a])) 
                run -> children[c - a] = new TrieNode();
            run = run -> children[c - a];
        }
        run -> isKey = 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 query(word.c_str(), root);
    }

private:
    TrieNode* root;

    bool query(const char* word, TrieNode* node) {
        TrieNode* run = node;
        for (int i = 0; word[i]; i++) {
            if (run && word[i] != .)
                run = run -> children[word[i] - a];
            else if (run && word[i] == .) { 
                TrieNode* tmp = run;
                for (int j = 0; j < 26; j++) {
                    run = tmp -> children[j];
                    if (query(word + i + 1, run))
                        return true;
                }
            }
            else break;
        }
        return run && run -> isKey; 
    }
};
View Code

 

一刷leetcode——数据结构