思路:

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

 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. systemtap 作用-- SystemTap使用技巧

    http://blog.csdn.net/wangzuxi/article/details/42849053

  2. 打造Spring Cloud构建微服务架构的最全资料

    访问: https://git.oschina.net/didispace/SpringCloud-Learning http://blog.didispace.com/categories/Spri ...

  3. seajs入门使用

    使用 Sea.js 进行模块化开发还能够带来非常多优点: 模块的版本号管理. 通过别名等配置,配合构建工具,能够比較轻松地实现模块的版本号管理. 提高可维护性.模块化能够让每一个文件的职责单一,很有利 ...

  4. ubuntu 中 iptables 和 ufw 的关系

    我突然发现,自己平常使用的 iptables 和 ufw 到底是啥关系?平常其实iptables和ufw在配置防火墙,开启端口是,还是偶尔会使用到的. 没去思考过这两者是啥关系,哎...,这就不够好了 ...

  5. MFC自己主动获取网络地址函数实现----广播地址,网关,子网掩码

    void CSetSignalBoxDlg::OnBnClickedButtonGetbroadcastaddr() {       //凝视部分为还有一种获取IP方式,可略过 //char Name ...

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

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

  7. 工作总结 EF GroupBy() Select() Select() 中 Count() 分组 求总

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. [转] Ubuntu/Linux Mint/Debian 安装 Java 8

    本PPA由webupd8制作,支持Ubuntu .04以及对应的Linux Mint版本,Oracle Java 8包提供JDK8 和 JRE8. sudo add-apt-repository pp ...

  9. solr入门之solr的拼写检查功能的应用级别尝试

    今天主要是收集了些拼写检查方面的资料和 尝试使用一下拼写检查的功能--=遇到了不少问题 拼写检查的四种配置眼下我仅仅算是成功了半个吧 --------------------------------- ...

  10. Android开发之接收系统广播消息

    BroadcastReceiver除了接收用户所发送的广播消息之外.另一个重要的用途:接收系统广播. 假设应用须要在系统特定时刻运行某些操作,就能够通过监听系统广播来实现.Android的大量系统事件 ...