【重点BFS】LeetCode 127. Word Ladder

LeetCode 127. Word Ladder

Solution1:我的超过40%的AC的答案
原先利用BFS做但是内存溢出未能AC;进过修改加上了标记是否访问过的visited数组,可以AC啦~~~

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        if (find(wordList.begin(), wordList.end(), endWord) == wordList.end())
            return 0;
        queue<string> str_queue;
        str_queue.push(beginWord);
        int level = 2, cur = 1, next = 0;
        vector<int> visited(wordList.size(), 0);
        while (cur) { 
            string temp = str_queue.front();
            for (int i = 0; i < wordList.size(); i++) {
                if (! visited[i] && diff_n_char(temp, wordList[i]) == 1) {
                    visited[i] = 1;
                    if (wordList[i] == endWord)
                        return level;
                    str_queue.push(wordList[i]);
                    next++;
                }
            }
            str_queue.pop();
            cur--;
            if (!cur) {
                cur = next;
                next = 0;
                level++;
            }                
        }
        return 0;
    }

private:
    int diff_n_char(string& s1, string& s2) {
        int diff = 0;
        for (int i = 0; i < s1.size(); i++) {
            if (s1[i] != s2[i])
                diff++;
        }
        return diff;
    }
};

Solution2:
单向BFS,参考自花花酱:http://zxi.mytechroad.com/blog/searching/127-word-ladder/

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> dict(wordList.begin(), wordList.end());        
        if (!dict.count(endWord)) return 0;

        queue<string> q;
        q.push(beginWord);

        int l = beginWord.length();
        int step = 0;

        while (!q.empty()) {
            ++step;
            for (int size = q.size(); size > 0; size--) {                
                string w = q.front();                
                q.pop();
                for (int i = 0; i < l; i++) {                
                    char ch = w[i];
                    for (int j = 'a'; j <= 'z'; j++) {
                        w[i] = j;
                        // Found the solution
                        if (w == endWord) return step + 1;
                        // Not in dict, skip it
                        if (!dict.count(w)) continue;
                        // Remove new word from dict
                        dict.erase(w);
                        // Add new word into queue
                        q.push(w);                    
                    }
                    w[i] = ch;
                }
            }
        }
        return 0;
    }
};

//仿写答案
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> word_set(wordList.begin(), wordList.end());
        if (!word_set.count(endWord)) //endWord不在wordList中
            return 0;
        queue<string> word_q;
        word_q.push(beginWord);
        int level = 1, len_word = beginWord.size();
        while (!word_q.empty()) {
            level++;
            for (int i = word_q.size(); i > 0; i--) {
                //倒着计数很聪明,简化了后面queue增、删元素引起的边界问题
                string w = word_q.front();
                word_q.pop();
                for (int j = 0; j < len_word; j++) {
                    char temp_ch = w[j];
                    for (int k = 'a'; k <= 'z'; k++) {
                        w[j] = k;
                        if (w == endWord) return level;
                        if (!word_set.count(w)) continue;
                        word_set.erase(w);
                        word_q.push(w);
                    }
                    w[j] = temp_ch;
                }
            }
        }
        return 0;
    }
};

Solution3:
双向BFS,参考自花花酱:http://zxi.mytechroad.com/blog/searching/127-word-ladder/

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> dict(wordList.begin(), wordList.end());        
        if (!dict.count(endWord)) return 0;

        int l = beginWord.length();

        unordered_set<string> q1{beginWord};
        unordered_set<string> q2{endWord};

        int step = 0;

        while (!q1.empty() && !q2.empty()) {
            ++step;

            // Always expend the smaller queue first
            if (q1.size() > q2.size())
                std::swap(q1, q2);

            unordered_set<string> q;

            for (string w : q1) {
                for (int i = 0; i < l; i++) {
                    char ch = w[i];
                    for (int j = 'a'; j <= 'z'; j++) {
                        w[i] = j;
                        if (q2.count(w)) return step + 1;
                        if (!dict.count(w)) continue;                        
                        dict.erase(w);
                        q.insert(w);
                    }
                    w[i] = ch;
                }
            }

            std::swap(q, q1);
        }

        return 0;
    }
};

猜你喜欢

转载自blog.csdn.net/allenlzcoder/article/details/81074337
今日推荐