leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
https://leetcode.com/problems/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 intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Return
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Note:
- All words have the same length.
- All words contain only lowercase alphabetic characters.
class Solution {
public:
void dfs(vector<vector<string> >& res, unordered_map<string, vector<string> >& fa, vector<string> load, string beginWord, string curWord) {
if(curWord == beginWord) {
reverse(load.begin(), load.end());
res.push_back(load);
reverse(load.begin(), load.end());
return;
}
for(int i=; i<fa[curWord].size(); ++i) {
load.push_back(fa[curWord][i]);
dfs(res, fa, load, beginWord, fa[curWord][i]);
load.pop_back();
}
}
vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
vector<vector<string> > res;
if(beginWord.compare(endWord) == ) return res;
wordList.insert(endWord);
unordered_map<string, vector<string> > fa;
unordered_set<string> vis;
unordered_set<string> lev;
unordered_set<string> next_lev;
lev.insert(beginWord);
bool found = false;
while(!lev.empty() && !found) {
if(lev.find(endWord) != lev.end()) found = true;
for(auto str: lev) vis.insert(str);
for(auto str : lev) {
for(int i=; i<str.length(); ++i) {
for(char ch = 'a'; ch <= 'z'; ++ch) {
if(str[i] != ch) {
string tmp = str;
tmp[i] = ch;
if(wordList.find(tmp) != wordList.end() && vis.find(tmp) == vis.end()) {
next_lev.insert(tmp);
fa[tmp].push_back(str);
}
}
}
}
}
lev.clear();
swap(lev, next_lev);
}
if(found) {
vector<string> load;
load.push_back(endWord);
dfs(res, fa, load, beginWord, endWord);
}
return res;
}
};
leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)的更多相关文章
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Leetcode#126 Word Ladder II
原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
随机推荐
- Weblogic下部署的应用,当更新文件时需要重新安装部署
JSP页面检查(秒):-1 Servlet重新加载检查(秒):-1 -1说明从不检查,故当更新文件时,需要重新部署,或重新安装部署.
- CentOS笔记——配置DNS服务器
前话 咳咳,这次Linux系统的DNS服务器搭建我不得不记下来.,这错误真的太蛋疼了,我整整弄了两天才解决问题(抱歉我很蠢). 也许有人会和我犯同样的错误,给大家分享一下经验. 首先总结一下知识点: ...
- JS动画 | 用TweenMax实现收集水滴效果
之前在CodePen上接触了TweenMax, 被它能做到的酷炫效果震撼了. (文末放了5个GSAP的效果GIF) 最近要做一个"收集水滴"的动效, 于是就试用了一下TweenMa ...
- cache设计,以及多核造成的不一致性以及解决方案
http://www.360doc.com/content/11/1013/00/1317564_155625188.shtml http://blog.csdn.net/muxiqingyang/a ...
- 24点C++程序实现 编程之美1.16
解法1,对于任意输入的四个数字,给出一个24点的解法,若无解,则没有输出. 原理参照下图(编程之美原书) 代码如下,仅供参考 // 1.16.cpp : Defines the entry point ...
- java文档注释主要使用方法
一.java包含哪些注释 1.//用于单行注释. 2./*...*/用于多行注释,从/*开始,到*/结束,不能嵌套. 3./**...*/则是为支持jdk工具javadoc.exe而特有的注释语句.这 ...
- 2.2linux内核移植简介
1,编译linux3.5出错 root@phone-desktop:/opt/FriendlyARM/tiny4412/Linux/linux-3.5# makescripts/kconfig/con ...
- Android开发之内容观察者
内容观察者: 当关注应用的数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知 实现步骤: 1.假如是自定义的ContentProvider,需要 ...
- Struts个人总结
编写Struts2第一个程序 Struts2是目前最流行的MVC框架,吸收了传统Struts和WebWork两者的精华,基于Struts2来进行开发可以大大减少开发时间,提高开发效率,并降低后期维护时 ...
- poj3067
求交点的个数: 容易发现,对于两条航线(xi,yi)和(xj,yj),设xi<xj 只有yi>yj时两条航线存在交点: 于是我们考虑以x为第一关键字减序,y为第二关键字为减序排序: 则对于 ...