LeetCode126:Word Ladder
题目:
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.
- 解题思路:
- 这题一开始没啥思路,谷歌一下,才知要用到BFS,既然要用到BFS,那当然形成一个抽象图。这里我们将每一个字符串当做图中一节点,如果两字符串只需通过变化一个字符即可相等,我们认为这两字符串相连。
- 遍历图中节点时,我们通常会利用一个visit还标识是否访问过,这里我们将处理过的节点直接从dict中删除,以免重复处理。
- 实现代码:
#include <iostream>
#include <string>
#include <queue>
#include <unordered_set>
using namespace std; /*
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.
*/
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(start.empty() || end.empty() || dict.empty())
return 0;
queue<string> squ[2];//这里需要用到两个队列,因为是bfs,按层遍历,所以需要一层一层进行处理
squ[0].push(start);
bool qid = false;
int minLen = 1;
while(!squ[qid].empty())
{
while(!squ[qid].empty())//处理同一层节点
{
string curstr = squ[qid].front();
squ[qid].pop();
for(int i = 0; i < curstr.size(); i++)
{ for(char j = 'a'; j <= 'z'; j++)
{
if(j == curstr[i])
continue;
char t = curstr[i];
curstr[i] = j;
if(curstr == end)
{
return minLen+1;
} if(dict.count(curstr) > 0)
{
squ[!qid].push(curstr);
dict.erase(curstr);
}
curstr[i] = t;
} } }
qid = !qid;//表示将要处理的下一层
minLen++; }
return 0; }
}; int main(void)
{
string start("hit");
string end("cog");
unordered_set<string> dict;
dict.insert("hot");
dict.insert("dot");
dict.insert("dog");
dict.insert("lot");
dict.insert("log");
Solution solution;
int min = solution.ladderLength(start, end, dict);
cout<<min<<endl;
return 0;
}
LeetCode126:Word Ladder的更多相关文章
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- leetcode笔记:Word Ladder
一. 题目描写叙述 Given two words (start and end), and a dictionary, find the length of shortest transformat ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
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:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- 算法 - 求两个自然数的最小公倍数(C++)
//************************************************************************************************** ...
- XCode使用自带SVN,SVN命令
转载http://blog.sina.com.cn/s/blog_68661bd80101phpy.html 这两天响应老板要求,把所有代码放到公司的SVN服务器上,按照我的想法肯定是就苹果组建一个服 ...
- ODAC(V9.5.15) 学习笔记(十八) 数据集缓冲模式
数据集的缓冲模式(Cached mode)是将数据库服务器的数据缓冲在客户端内存中进行处理,不再依赖服务器.只有当数据需要提交数据库服务器进行保存时,才将变更数据一次性提交数据库服务器. 数据集缓冲模 ...
- 未知高度定宽div水平居中及垂直居中(兼容ie6及其他牛逼浏览器)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 重构第2天:方法搬移(Move Method)
现在就重构来说是非常普通的,虽然我们经常会漏掉或忽略一些需要重构的地方.方法搬移,正如所定义的那样,把方法搬移到更适合他的位置.让我们看看下面这一段重构前的代码: 理解:方法搬移,正如所定义的那样,把 ...
- [Android Studio 权威教程]断点调试和高级调试
好了开始写一个简单的调试程序,我们先来一个for循环 ? 1 2 3 4 5 6 7 8 <code class="language-java hljs ">for ( ...
- Virgo Tomcat Server 指南-Hello World
Eclipse发布了最新的Virgo Tomccat Server.VTS是一个应用服务器与OSGi紧密结合并且可以开发bundles形式的Spring web apps应用,他们同样拥有OSGi和S ...
- c++ 全局变量初始化的一点总结
注意:本文所说的全局变量指的是 variables with static storage,措词来自 c++ 的语言标准文档. 什么时候初始化 根据 C++ 标准,全局变量的初始化要在 main 函数 ...
- Lua面向对象设计
首先对于Lua语言,它没有打算被用来进行大型的程序设计,相反,Lua目标定于小型到中型的程序设计,通常是作为大型系统的一部分,所以它只提供了一套精简的元素,很多高级语言的概念都没有.这样Lua就成为了 ...
- 游戏开发工具之纹理打包器-3.使用GDI+绘图
上一次我们实现了把我们要的图片添加到CTreeCtrl控件里去,并显示图片的缩略图,现在开始我们要讲比较重要的部分--绘图区.为了实现能编辑图片的功能,绘图区应该具有如下功能. 1. 添加删除图片. ...