思路:

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

  1. class Solution
  2. {
  3. public:
  4. void getPath(string now, string beginWord, string endWord, vector<string>& buf, unordered_map<string, unordered_set<string>>& par, vector<vector<string>>& ret)
  5. {
  6. if (now == beginWord)
  7. {
  8. vector<string> tmp(, endWord);
  9. for (auto it : buf) tmp.push_back(it);
  10. ret.push_back(tmp);
  11. reverse(ret.back().begin(), ret.back().end());
  12. return;
  13. }
  14. for (auto it : par[now])
  15. {
  16. buf.push_back(it);
  17. getPath(it, beginWord, endWord, buf, par, ret);
  18. buf.pop_back();
  19. }
  20. }
  21. vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList)
  22. {
  23. unordered_set<string> tmp;
  24. for (auto it : wordList) tmp.insert(it);
  25. unordered_map<string, unordered_set<string>> par;
  26. unordered_set<string> q;
  27. q.insert(beginWord);
  28. bool flg = false;
  29. while (!q.empty())
  30. {
  31. unordered_set<string> next;
  32. for (auto it : q)
  33. {
  34. for (int i = ; i < it.length(); i++)
  35. {
  36. for (char c = 'a'; c <= 'z'; c++)
  37. {
  38. string buf = it;
  39. if (buf[i] == c) continue;
  40. buf[i] = c;
  41. if (!tmp.count(buf)) continue;
  42. if (!q.count(buf))
  43. {
  44. next.insert(buf);
  45. par[buf].insert(it);
  46. }
  47. if (buf == endWord) flg = true;
  48. }
  49. }
  50. }
  51. for (auto it : q) { tmp.erase(it); }
  52. q = next;
  53. if (flg) break;
  54. }
  55. vector<vector<string>> ret;
  56. if (flg)
  57. {
  58. vector<string> buf;
  59. getPath(endWord, beginWord, endWord, buf, par, ret);
  60. }
  61. return ret;
  62. }
  63. };

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又是如何解决幻读的?

    一.什么是幻读 在一次事务里面,多次查询之后,查询的结果集的个数不一致的情况叫做幻读.而多出来或者少的哪一行被叫做 幻行 二.为什么要解决幻读 在高并发数据库系统中,需要保证事务与事务之间的隔离性,还 ...

  2. 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列

    ) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...

  3. git锁和钩子以及图形化界面

    1.锁机制 Locking Options 严格锁(strict locking):一个时刻,只有一个人可以占用资源. 乐观锁(optimistic locking):允许多个人同时修改同一文件.乐观 ...

  4. jQuery源代码解析(1)—— jq基础、data缓存系统

    闲话 jquery 的源代码已经到了1.12.0版本号.据官网说1版本号和2版本号若无意外将不再更新,3版本号将做一个架构上大的调整.但预计能兼容IE6-8的.或许这已经是最后的样子了. 我学习jq的 ...

  5. 【python】一些好的学习网址

    http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...

  6. python中的is判断引用的对象是否一致,==判断值是否相等

    python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...

  7. 关于jQuery写插件及其演示

    关于写jQuery插件是非常有必要的.这是前端学习其中必须经过的一个过程 对于初次写插件先想清楚原理       (function($){  $.fn.yourName = function(opt ...

  8. SPOJ 15. The Shortest Path 最短路径题解

    本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...

  9. jeasyUI treegrid 的 reload 和 getChanges

    看上去,treegrid继承自datagrid,因此,datagrid有的,treegrid也会有. 比如说,getChanges,翻看那些网络教程,绝大多数都没提treegrid有这个东东.但是,t ...

  10. 2016/1/20 笔记 1, 包 引入 static 已经补充到类里 2,继承

    继承  1,关键字 extends       2,子类自动继承父类非私有的属性和方法 也叫成员变量 成员方法       3,super代表的是父类 调用父类的方法 默认在构造函数中生成      ...