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 ...
随机推荐
- return false取消手机移动端的默认设置
想做一个语音界面,当长按语音按钮的时候,总会出现移动端什么复制粘贴菜单.然后在JS中加入return false后就消失了,感觉好神奇哦~
- 目前几款基于html5的前端框架:如Bootstrap、Foundation、Semantic UI 、Amaze UI
Bootstrap是由Twitter在2011年8月推出的开源WEB前端框架,集合CSS 和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导 ...
- 使用ContentProvider管理多媒体-----查看多媒体数据中的所有图片
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- GFS: Evolution on Fast-forward
GFS: Evolution on Fast-forward by Marshall Kirk McKusick, Sean Quinlan | August 7, 2009 A discussion ...
- 各种主流数据库的比较(所以说我觉得Oracle这个keng?入的不错?)
随着计算机技术不断发展,各种数据库编程工具也随着发展,使当今的大多数程序开发人员可以摆脱枯燥无味的用计算机指令或汇编语言开发软件,而是利用一系列高效的.具有良好可视化的编程工具去开发各种数据库软件,从 ...
- bzoj 1823: [JSOI2010]满汉全席
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ],next[ ...
- 二模 (11) day2
第一题: 题目大意: 有一本n个单词的字典,其中每个单词的长度不超过4且大于0.现在给你一篇文章,文章中没有分隔符,只有小写字母.现在需要你修改最少的字母,使文章(长度为m 是由字典中的单词构成. n ...
- opencv实现图片缩放
源码 #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/img ...
- Oracle常用的函数
1.常用的函数分为五大类: 字符函数.数字和日期函数.数字函数.转换函数.混合函数 2.字符函数 字符函数主要用于修改字符列.这些函数接受字符输入,返回字符或数字值.Oracle 提供的一些字符函数如 ...
- JNI与NDK简介
最近稍微了解一下JNI和NDK. 网上各种教程给人一种二者不分的感觉, 经过自己运行代码, 将两者的关系理了一下. 就目前了解,JNI应该是java自带的一种调用c和c++等语言(native cod ...