Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord toendWord, 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:
vector<vector<string> > findLadders(string start, string end,
const unordered_set<string> &dict) {
unordered_set < string > visited; // 判重
unordered_map<string, vector<string> > father; // 树
unordered_set<string> current, next; // 当前层,下一层,用集合是为了去重
bool found = false;
current.insert(start);
visited.insert(start);
while (!current.empty() && !found) {
for (auto word : current){
visited.insert(word);
}
for (auto word : current) {
for (size_t i = ; i < word.size(); ++i) {
string new_word = word;
for (char c = 'a'; c <= 'z'; ++c) {
if (c == new_word[i])
continue;
swap(c, new_word[i]);
if (new_word == end)
found = true; //找到了
if (visited.count(new_word) ==
&& (dict.count(new_word) || new_word == end)) {
next.insert(new_word);
father[new_word].push_back(word);
}
swap(c, new_word[i]); // restore
}
}
}
current.clear();
swap(current, next);
}
vector < vector<string> > result;
if (found) {
vector < string > path;
buildPath(father, path, start, end, result);
}
return result;
}
private:
void buildPath(unordered_map<string, vector<string> > &father,
vector<string> &path, const string &start, const string &word,
vector<vector<string> > &result) {
path.push_back(word);
if (word == start) {
result.push_back(path);
reverse(result.back().begin(),result.back().end());
} else {
for (auto f : father[word]) {
buildPath(father, path, start, f, result);
path.pop_back();
}
}
}
};
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 ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- eclipse设置总结
1.java函数折叠: windows->perferences->General->Editors->Structured Text Editors windows-> ...
- Huffman树与编码的简单实现
好久没写代码了,这个是一个朋友问的要C实现,由于不会C,就用JAVA写了个简单的.注释掉的代码属性按照原来朋友发的题里带的参数,发现没什么用就给注释掉了. package other; import ...
- 使用Matrix控制图像或组件变换的步骤
1.获取Matrix对象,该Matrix对象既可新创建,也可直接获取其他对象内封装的Matrix(例如Transformation对象内部) 2.调用Matrix的方法进行平移.旋转.缩放.倾斜等. ...
- Jquery操作复选框(CheckBox)的取值赋值实现代码
赋值 复选框 CheckBox 遍历 取值 1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $(&q ...
- TaskTracker启动过程源码级分析
TaskTracker也是作为一个单独的JVM来运行的,其main函数就是TaskTracker的入口函数,当运行start-all.sh时,脚本就是通过SSH运行该函数来启动TaskTracker的 ...
- ROS程序编辑器
我找到的比较好用的ROS代码编辑器,对于emacs和vim等神级编辑器不能自动补全,对于我这种新手编译出错都是字母打错了, 因此果断回避,找到了一款叫做code blocks的编辑器,在软件中心就能下 ...
- 应用程序池“Classic .NET AppPool”将被自动禁用
原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28972779&id=3868008 服务器 Server 20 ...
- 无刷新 checkbox列表的删除
前台 JS : function ModelDelete() { var checkvalues = null; var checValue = $("#dom1").find(& ...
- Activity(活动)-再讲
通过多天的学习,大家也了解了adb.exe 是用来进行 客户端(pc)-服务器端(android) 数据交互的. 用户可以使用工具Eclipse 中DDMS 隐示使用 adb.exe 进行连接,也可 ...
- ajax 请求超过了5s 还没有返回 的话 就自动取消
ajax请求时有个参数可以借鉴一下 var ajaxTimeOut = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : ' ...