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 ...
随机推荐
- mysql导入大型sql文件时注意事项
原文:http://blog.csdn.net/k21325/article/details/70808563 大型sql文件,需要在my.ini(windows)或者my.cnf(Linux)中设置 ...
- PopupMenu的演示样例
弹出菜单是停靠在一个View上的一个模式菜单. 假设View对象下方有空间,那么弹出菜单将显示在停靠对象的下方,否则会显示在上方. 这是很实用的: 源代码地址:http://download.csdn ...
- laravel有用的方法
1.tinker 造假数据 factory('App\User',3)->create(); DB::table 返回collection,可以用collection中的很多方法 比如-> ...
- Hadoop-mapreduce 程序在windows上执行需要注意的问题
1.在主程序中需要添加这几个参数配置 Configuration conf = new Configuration(); // 1.设置job运行时要访问的默认文件系统 conf.set(" ...
- ASP.NET Boilerplate 学习 AspNet Core2 浏览器缓存使用 c#基础,单线程,跨线程访问和线程带参数 wpf 禁用启用webbroswer右键菜单 EF Core 2.0使用MsSql/MySql实现DB First和Code First ASP.NET Core部署到Windows IIS QRCode.js:使用 JavaScript 生成
ASP.NET Boilerplate 学习 1.在http://www.aspnetboilerplate.com/Templates 网站下载ABP模版 2.解压后打开解决方案,解决方案目录: ...
- scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...
- 扩展gcd求解二元不定方程及其证明
#include <cstdio> #include <iostream> using namespace std; /*扩展gcd证明 由于当d = gcd(a,b)时: d ...
- ugc pgc ogc web2.0 mgc
http://yjy.people.com.cn/n/2014/0120/c245079-24169402.html machine
- (转)web会话管理方式
阅读目录 1. 基于server端session的管理 2. cookie-based的管理方式 3. token-based的管理方式 4. 安全问题 5. 总结 http是无状态的,一次请求结束, ...
- iOS方法重写
在O-C中子类可以继承父类的方法 ,而不需要从新编写相同的方法,但是有有时候子类并不想原封不动的继承父类的方法,而且是想做一些修改,这就采用啦方法的重写,方法从写有叫做方法覆盖,若子类的中的方法与父类 ...