leetcode126 Word Ladder II
思路:
宽搜过程中分层记录路径,递归还原。
实现:
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的更多相关文章
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- go语言中的timer 和ticker定时任务
https://mmcgrana.github.io/2012/09/go-by-example-timers-and-tickers.html --------------------------- ...
- android仿qq空间、微信朋友圈图片展示
废话不多说,先上效果图 由于近期须要做朋友圈功能,所以在此记录一下,事实上非常多人不明确的一点应该是在图片的排列上面吧,不规则的排列,事实上非常easy的.就是一个GridView.然而你xml光光写 ...
- Mac使用技巧之Finder的个人收藏
当使Finder的时候,左側会列出来个人收藏,能够非常方便的打开对应的文件夹.那么怎样把自己新建的文件夹也增加到个人收藏呢? 1.默认的个人收藏 2.新建名字为my_ios_demo文件夹,拖动这个文 ...
- Cardboard虚拟现实开发初步(二)
Google Cardboard 虚拟现实眼镜开发初步(二) Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一 ...
- phpmywind教程:关于日期函数调用整理
近期群里一直在问phpmywind的日期函数怎么调用,今天抽出时间给大家整理出来. 以月/日格式显示: <?php echo MyDate('m-d', $row['posttime']); ? ...
- ORACLE取周、月、季、年的開始时间和结束时间
1 取周的開始时间和结束时间 取周的開始时间.以星期一为開始. SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY ...
- HDU 1226 超级password
跟POJ 1465 multiple 类是.仅仅只是多了2个条件,长度不能超过500.还有就是 可能不是十进制. bfs+同余定理,就是用 mod 来判重. G++ 15ms 每次枚举一位,然后记录下 ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- Wordpress3.9开启多网站配置配置nginx进行局域网測试.
由于须要帮staff迁移一些数据, 所以想到了使用wordpress的多网站. 这个功能在wordpress3.0后就有了. 软件系统等信息: OS: linux debian wheezy php ...
- Keys.BACKSPACE Keys.SPACE
browser.find_element_by_xpath(xp_newpage).send_keys(Keys.SPACE)browser.find_element_by_xpath(xp_newp ...