思路:

宽搜过程中分层记录路径,递归还原。
实现:

 class Solution
{
public:
void getPath(string now, string beginWord, string endWord, vector<string>& buf, unordered_map<string, unordered_set<string>>& par, vector<vector<string>>& ret)
{
if (now == beginWord)
{
vector<string> tmp(, endWord);
for (auto it : buf) tmp.push_back(it);
ret.push_back(tmp);
reverse(ret.back().begin(), ret.back().end());
return;
}
for (auto it : par[now])
{
buf.push_back(it);
getPath(it, beginWord, endWord, buf, par, ret);
buf.pop_back();
}
}
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string> tmp;
for (auto it : wordList) tmp.insert(it);
unordered_map<string, unordered_set<string>> par;
unordered_set<string> q;
q.insert(beginWord);
bool flg = false;
while (!q.empty())
{
unordered_set<string> next;
for (auto it : q)
{
for (int i = ; i < it.length(); i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
string buf = it;
if (buf[i] == c) continue;
buf[i] = c;
if (!tmp.count(buf)) continue;
if (!q.count(buf))
{
next.insert(buf);
par[buf].insert(it);
}
if (buf == endWord) flg = true;
}
}
}
for (auto it : q) { tmp.erase(it); }
q = next;
if (flg) break;
}
vector<vector<string>> ret;
if (flg)
{
vector<string> buf;
getPath(endWord, beginWord, endWord, buf, par, ret);
}
return ret;
}
};

leetcode126 Word Ladder II的更多相关文章

  1. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  2. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  3. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  4. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  5. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  6. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  7. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  8. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  9. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. MySQL主从复制搭建教程收集(待实践)

    先收集一下,后续再搭建测试. https://zhangge.net/4019.html http://www.cnblogs.com/jiangwenju/p/6098974.html http:/ ...

  2. Eclipse 搭建tomcat+动态项目完整版

    1. Tomcat搭建 1.新加服务器,右击控制台的server目录->new->server->选择本地tomcat 2.配置tomcat属性(如果更改失败,将tomcat下的项目 ...

  3. android_handler(二)

    这篇记录 android 消息机制中.WorkThread 模拟向网络訪问数据,获得数据后,返回 message 发送给 MainThread ,并改动 TextView 的 text 的这种一个步骤 ...

  4. 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体server笔记(十)

    第十部分 -- 开发板測试 前几天已经分别将nginx和ffmpeg移植到了开发板上面.可是还是没有进行不论什么的測试并不知道移植后的效果怎样. 今天分别做了两个測试.证明移植的结果是可用的. 1.測 ...

  5. HDOJ 5381 The sum of gcd 莫队算法

    大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...

  6. MariaDB ----单表查询

    1>按一定条件查询某字段的数据 (where) ; ( 查询 id > 的数据) #补充: ; 注意“ select * from  students1:  (此命令需谨慎使用, 数据量大 ...

  7. 树状数组 LA 4329 亚洲赛北京赛区题

    复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...

  8. 【Noip模拟By yxj】

    1.randomDescription 给定4个参数A0,N,c,p,你需要按下式构造A1~AN: A[i]=(A[i-1]2+c)mod p 之后,你需要求出A1~AN中,第K大的数值.Input ...

  9. 在Ubuntu 12.04 LTS下成功访问Windows域共享(mount //192.168.1.102/share -o user=DOMIAN\\user,pass=passwd /mnt)

    Ubuntu 12.04 LTS下成功访问Windows域共享: 1,在命令行模式下 mount //192.168.1.102/share -o user=DOMIAN\\user,pass=pas ...

  10. Android 通过USB查看kernel调试信息【转】

    本文转载自:http://blog.csdn.net/lindonghai/article/details/51683644 前提:电脑已安装adb并可正常使用. 在调试Android驱动时,需要查看 ...