LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
分析:这种题,肯定是每次改变单词的一个字母,然后逐渐搜索,很多人一开始就想到用dfs,其实像这种求最短路径、树最小深度问题bfs最适合,可以参考我的这篇博客bfs(层序遍历)求二叉树的最小深度。本题bfs要注意的问题:
- 和当前单词相邻的单词是:对当前单词改变一个字母且在字典中存在的单词
- 找到一个单词的相邻单词,加入bfs队列后,要从字典中删除,因为不删除的话会造成类似于hog->hot->hog的死循环。而删除对求最短路径没有影响,因为我们第一次找到该单词肯定是最短路径,即使后面其他单词也可能转化得到它,路径肯定不会比当前的路径短(如果要输出所有最短路径,则不能立即从字典中删除,具体见下一题)
- bfs队列中用NULL来标识层与层的间隔,每次碰到层的结尾,遍历深度+1
我们利用和求二叉树最小深度层序遍历的方法来进行bfs,代码如下: 本文地址
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//BFS遍历找到的第一个匹配就是最短转换,空字符串是层与层之间的分隔标志
queue<string> Q;
Q.push(start); Q.push("");
int res = ;
while(Q.empty() == false)
{
string str = Q.front();
Q.pop();
if(str != "")
{
int strLen = str.length();
for(int i = ; i < strLen; i++)
{
char tmp = str[i];
for(char c = 'a'; c <= 'z'; c++)
{
if(c == tmp)continue;
str[i] = c;
if(str == end)return res+;
if(dict.find(str) != dict.end())
{
Q.push(str);
dict.erase(str);
}
}
str[i] = tmp;
}
}
else if(Q.empty() == false)
{//到达当前层的结尾,并且不是最后一层的结尾
res++;
Q.push("");
}
}
return ;
}
};
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["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.
分析:本题主要的框架和上一题是一样,但是还要解决两个额外的问题:一、 怎样保证求得所有的最短路径;二、 怎样构造这些路径
第一问题:
- 不能像上一题第二点注意那样,找到一个单词相邻的单词后就立马把它从字典里删除,因为当前层还有其他单词可能和该单词是相邻的,这也是一条最短路径,比如hot->hog->dog->dig和hot->dot->dog->dig,找到hog的相邻dog后不能立马删除,因为和hog同一层的单词dot的相邻也是dog,两者均是一条最短路径。但是为了避免进入死循环,再hog、dot这一层的单词便利完成后dog还是得从字典中删除。即等到当前层所有单词遍历完后,和他们相邻且在字典中的单词要从字典中删除。
- 如果像上面那样没有立马删除相邻单词,就有可能把同一个单词加入bfs队列中,这样就会有很多的重复计算(比如上面例子提到的dog就会被2次加入队列)。因此我们用一个哈希表来保证加入队列中的单词不会重复,哈希表在每一层遍历完清空(代码中hashtable)。
- 当某一层的某个单词转换可以得到end单词时,表示已经找到一条最短路径,那么该单词的其他转换就可以跳过。并且遍历完这一层以后就可以跳出循环,因为再往下遍历,肯定会超过最短路径长度
第二个问题:
- 为了输出最短路径,我们就要在比bfs的过程中保存好前驱节点,比如单词hog通过一次变换可以得到hot,那么hot的前驱节点就包含hog,每个单词的前驱节点有可能不止一个,那么每个单词就需要一个数组来保存前驱节点。为了快速查找因此我们使用哈希表来保存所有单词的前驱路径,哈希表的key是单词,value是单词数组。(代码中的unordered_map<string,vector<string> >prePath)
- 有了上面的前驱路径,可以从目标单词开始递归的构造所有最短路径(代码中的函数 ConstructResult)
class Solution {
public:
typedef unordered_set<string>::iterator HashIter;
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Note: The Solution object is instantiated only once and is reused by each test case.
queue<string> Q;
Q.push(start); Q.push("");
bool hasFound = false;
unordered_map<string,vector<string> >prePath;//前驱路径
unordered_set<string> hashtable;//保证bfs时插入队列的元素不存在重复
while(Q.empty() == false)
{
string str = Q.front(), strCopy = str;
Q.pop();
if(str != "")
{
int strLen = str.length();
for(int i = ; i < strLen; i++)
{
char tmp = str[i];
for(char c = 'a'; c <= 'z'; c++)
{
if(c == tmp)continue;
str[i] = c;
if(str == end)
{
hasFound = true;
prePath[end].push_back(strCopy);
//找到了一条最短路径,当前单词的其它转换就没必要
goto END;
}
if(dict.find(str) != dict.end())
{
prePath[str].push_back(strCopy);
//保证bfs时插入队列的元素不存在重复
if(hashtable.find(str) == hashtable.end())
{Q.push(str); hashtable.insert(str);}
}
}
str[i] = tmp;
}
}
else if(Q.empty() == false)//到当前层的结尾,且不是最后一层的结尾
{
if(hasFound)break;
//避免进入死循环,把bfs上一层插入队列的元素从字典中删除
for(HashIter ite = hashtable.begin(); ite != hashtable.end(); ite++)
dict.erase(*ite);
hashtable.clear();
Q.push("");
}
END: ;
}
vector<vector<string> > res;
if(prePath.find(end) == prePath.end())return res;
vector<string> tmpres;
tmpres.push_back(end);
ConstructResult(prePath, res, tmpres, start, end);
return res;
}
private:
//从前驱路径中回溯构造path
void ConstructResult(unordered_map<string,vector<string> > &prePath,
vector<vector<string> > &res, vector<string> &tmpres,
string &start, string &end)
{
if(start == end)
{
reverse(tmpres.begin(), tmpres.end());
res.push_back(tmpres);
reverse(tmpres.begin(), tmpres.end());
return;
}
vector<string> &pre = prePath[end];
for(int i = ; i < pre.size(); i++)
{
tmpres.push_back(pre[i]);
ConstructResult(prePath, res, tmpres, start, pre[i]);
tmpres.pop_back();
}
}
};
另外这一题如果不用队列来进行bfs,可能会更加方便,使用两个哈希表来模拟队列,这样还可以避免前面提到的同一个元素加入队列多次的问题,具体可以参考这篇博客
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3443512.html
LeetCode:Word Ladder I II的更多相关文章
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- Word Ladder I & II
Word Ladder I Given two words (start and end), and a dictionary, find the length of shortest transfo ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- Windows8.1下安装NoSQL-- mongodb安装使用
1. 官方下载monodb:http://www.mongodb.org/downloads 现在最新版本3.0 2. 以下载Windows 64-bit为例官方最新版的没有分开, 32位和64位是应 ...
- Struts2(2) —— Action
Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器拦截请求,创建代理Action对象,执行方法 ...
- iOS用三种途径实现一方法有多个返回值
以前觉得这种标题有点偏向于理论,实际开发中怎么会有这种诡异的需求,但是真正遇到了这种硬需求时觉得还是有那么点价值的,理论付诸了实践在此也就做了个整理. 以我私下开发中的一处代码为例,本意是希望有这么一 ...
- iOS 中常用的对密码进行MD5加密
iOS中MD5加密 标签(空格分隔): iOS MD5 + (NSString *)MD5:(NSString *)str { const char *cStr = [str UTF8String]; ...
- 使用 Async 和 Await 的异步编程(C# 和 Visual Basic)[msdn.microsoft.com]
看到Microsoft官方一篇关于异步编程的文章,感觉挺好,不敢独享,分享给大家. 原文地址:https://msdn.microsoft.com/zh-cn/library/hh191443.asp ...
- numpy中matrix的特殊属性
一.matrix特殊属性解释 numpy中matrix有下列的特殊属性,使得矩阵计算更加容易 摘自 NumPy Reference Release 1.8.1 1.1 The N-dimensiona ...
- Linux命令学习总结:chage
命令简介: 该命令用于密码时效管理.它可以修改账号和密码的有效期.对于chage命令的描述如下所示: The chage command changes the number of days betw ...
- SQL Server Column Store Indeses
SQL Server Column Store Indeses SQL Server Column Store Indeses 1. 概述 2. 索引存储 2.1 列式索引存储 2.2 数据编码和压缩 ...
- 把你的Project发布到GitHub上
在上一篇文章中说明了如何使用远程仓库,接下来,就使用常用远程仓库GitHub来管理你的project. 1)在GitHub上创建仓库 要使用GitHub,肯定要注册GitHub帐户,然后建立一个仓库. ...
- MongoDB学习笔记~批量插入方法的实现
回到目录 批量插入在EF时代大叔就自己封装过,原理是将多次SQL连接和多次向SQL发送的指令减少到1次,或者1000条数据1次,而对于EF产生的语句来说,这无疑是性能高效的,因为EF这边在处理时,每个 ...