• 126. 单词接龙 II(bfs)


     

    难度困难

    按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:

    • 每对相邻的单词之间仅有单个字母不同。
    • 转换过程中的每个单词 si1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。
    • sk == endWord

    给你两个单词 beginWord 和 endWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWord 到 endWord 的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 [beginWord, s1, s2, ..., sk] 的形式返回。

    示例 1:

    输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
    输出:[["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
    解释:存在 2 种最短的转换序列:
    "hit" -> "hot" -> "dot" -> "dog" -> "cog"
    "hit" -> "hot" -> "lot" -> "log" -> "cog"
    

    示例 2:

    输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
    输出:[]
    解释:endWord "cog" 不在字典 wordList 中,所以不存在符合要求的转换序列。
    

    提示:

    • 1 <= beginWord.length <= 7
    • endWord.length == beginWord.length
    • 1 <= wordList.length <= 5000
    • wordList[i].length == beginWord.length
    • beginWordendWord 和 wordList[i] 由小写英文字母组成
    • beginWord != endWord
    • wordList 中的所有单词 互不相同
    class Solution {
    public:
        vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        vector<vector<string>> res;
        unordered_set<string> us(wordList.begin(), wordList.end());
            if (us.find(endWord) == us.end())
                return res;
            unordered_set<string> visited;
            queue<vector<string>> q;
            visited.insert(beginWord);
            q.push({beginWord});
            int step = 0;
            while (!q.empty()) {
                int sz = q.size();
                for(int i = 0 ; i< sz; i++){
                    vector<string> cur = q.front(); q.pop();
                    //cout << cur << endl;
                    if (cur.back() == endWord) {
                        res.emplace_back(cur);
                        continue;
                    }
                    for (int j = 0; j < beginWord.size(); j++) {
                        string y = cur.back();
                        for (int k = 0 ; k < 26;k++) {
                            y[j] = 'a' + k;
                            if (y[j]==cur.back()[j]) {
                               continue;
                            }
                            if(us.find(y) != us.end() && visited.find(y) == visited.end()) {
                                cur.emplace_back(y);
                                q.push(cur);
                                visited.insert(y);
                                cur.pop_back();
                            }
                        }
                    }
                }
                step ++;
            }
            return res;
        }
    };
  • 相关阅读:
    【MFC】在CHtmlView中在同一窗口显示新打开页面
    【MFC】CHtmlView::GetSource中文乱码的问题
    【Win32】对指定进程进行禁音
    【MFC】在CHtmlView中准确判断页面加载完成
    【MFC】CHtmlView或WebBrowser禁止脚本错误提示
    【MFC】CDialogBar中按钮不可用
    【转载记录】Accessing Device Drivers from C#
    【源代码R3】移植一份LARGE_INTEGER转时间的代码
    Square Root of Permutation
    MySQL创建视图命令
  • 原文地址:https://www.cnblogs.com/zle1992/p/15679857.html
Copyright © 2020-2023  润新知