leetcode126 Word Ladder II

思路:

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

 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(1, 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 = 0; 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 };
时间: 2024-10-17 22:45:41

leetcode126 Word Ladder II的相关文章

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example,

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

[python leetcode] Word Ladder II (very hard)[非常难,放弃]

Word Ladder II 描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: • Only one letter can be changed at a time • Each intermediate word must exist in the dictionary For examp

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit

[Swift]LeetCode126. 单词接龙 II | Word Ladder II

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in the word list. Note

【leetcode】126. Word Ladder II

题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从startword开始到wordlist每一个word的最小转换次数,这一点非常重要,可以过滤很多无效的运算. 代码如下: class Solution(object): def getLadderList(self, w,d): l = [] r = [] for i in xrange(26): l.a