leetcode——Word Ladder

    好多天没写leetcode了,就是卡在这题上,然后各种偷懒。。。。之前思路一直没理顺,想了两个晚上,就放弃了,后来就去看答案去了。。。可是答案没有注释,有点看不太懂,后来只好上网查了,有的说用到trie树,就去看了trie树是啥。。。反正那段时间状态不好,总不想看,偶尔还被跑男吸引注意力,总之各种偷懒,到了今天终于看完答案了,试着结合各方答案写了一下。。调了2个小时,终于通过了。。。而且效率也不高。。。但不管怎样,终于做出来了。。。但是不知道为什么这么做能保证每个单词的步骤数得出来的是最小的。。。

    最后调了好久的地方是int possibleStep = step[word];这句,放错了,放到了    for (char c = 'a'; c <= 'z'; c++)之前,比如这时word是cet,字典里有bet, get,这时,bet的步数是2,possibleStep是2,这时继续循环,找到了get,其步数就是possibleStep++为3,这就不对了,所以每替换一个字母,都应该是在其原词的基础上++。

class Solution {
    // 用广度搜索,每次换一个字母,所得单词在字典里,就入队,直到找到end为止
public:
    typedef string state_t;
    int ladderLength(string start, string end,
        const unordered_set<string> &dict) {// 如果start和end字数不一样,或者start<1,则返回0
        if (start.size() < 1 || start.size() != end.size()) return 0;
        if (start.size() == 1) return 2;
        unordered_set<string> visited; // 用来记录已经访问过的单词
        queue<string> que;
        unordered_map<string, int> step; // 记录访问到那个单词时走了几步
        que.push(start); // 入队
        visited.insert(start); // 标记访问过
        step[start] = 1;
        while (!que.empty()){
            string word = que.front();// 出队
            que.pop();
            for (int i = 0; i != word.size(); i++){
                string newWord = word; // 替换字母前记得还原
                for (char c = 'a'; c <= 'z'; c++){
                    int possibleStep = step[word];
                    if (c == word[i]) continue;
                    newWord = newWord.replace(i, 1, 1, c); // 替换字母
                    if (newWord == end)
                        return possibleStep + 1;  // 若是end 则返回步数
                    // 若能在字典里找到且没有访问过
                    if (dict.count(newWord) != 0 && visited.count(newWord) == 0){
                        possibleStep++;
                        que.push(newWord);
                        visited.insert(newWord);
                        step[newWord] = possibleStep;
                    }
                }
            }
        }
        // 若找完所有的可能性都没返回,则说明找不到,则返回0
        return 0;
    }
};

    然后发现其实可以不用possibleStep,如果字典里找到且没有访问过,直接让其步数为其原词+1就行,不知道是不是网速的问题,我就这么改之后,由原先的1400+ms变成870+ms了,反正不管是不是网速的问题,应该确实是能快一些的。

修改后代码:

class Solution {
    // 用广度搜索,每次换一个字母,所得单词在字典里,就入队,直到找到end为止
public:
    typedef string state_t;
    int ladderLength(string start, string end,
        const unordered_set<string> &dict) {// 如果start和end字数不一样,或者start<1,则返回0
        if (start.size() < 1 || start.size() != end.size()) return 0;
        if (start.size() == 1) return 2;
        unordered_set<string> visited; // 用来记录已经访问过的单词
        queue<string> que;
        unordered_map<string, int> step; // 记录访问到那个单词时走了几步
        que.push(start); // 入队
        visited.insert(start); // 标记访问过
        step[start] = 1;
        while (!que.empty()){
            string word = que.front();// 出队
            que.pop();
            for (int i = 0; i != word.size(); i++){
                string newWord = word; // 替换字母前记得还原
                for (char c = 'a'; c <= 'z'; c++){
                    if (c == word[i]) continue;
                    newWord = newWord.replace(i, 1, 1, c); // 替换字母
                    if (newWord == end)
                        return step[word] + 1;  // 若是end 则返回步数
                    // 若能在字典里找到且没有访问过
                    if (dict.count(newWord) != 0 && visited.count(newWord) == 0){
                        que.push(newWord);
                        visited.insert(newWord);
                        step[newWord] = step[word] + 1;
                    }
                }
            }
        }
        // 若找完所有的可能性都没返回,则说明找不到,则返回0
        return 0;
    }
};
原文地址:https://www.cnblogs.com/skysand/p/4158666.html