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 ...
随机推荐
- [原]quick集成spine动画
更新说明: 新增了骨骼绑定node用法 参考:http://blog.csdn.net/n5/article/details/21795265 在SkeletonRenderer.h 和cpp里面新加 ...
- (笔记)Linux内核学习(十一)之I/O层和I/O调度机制
一 块I/O基本概念 字符设备:按照字符流的方式被有序访问的设备.如串口.键盘等. 块设备:系统中不能随机(不需要按顺序)访问固定大小的数据片(chunk 块)的设备. 如:硬盘.软盘.CD-ROM驱 ...
- 通过PowerShell获取Windows系统密码Hash
当你拿到了系统控制权之后如何才能更长的时间内控制已经拿到这台机器呢?作为白帽子,已经在对手防线上撕开一个口子,如果你需要进一步扩大战果,你首先需要做的就是潜伏下来,收集更多的信息便于你判断,便于有更大 ...
- URL 学习总结
1.绝对路径(以"/"斜线开头的路径,代表相对于当前Web应用): a)地址给服务器用,web应用名称可以省略. 请求包含:request.getRequestDispatcher ...
- OpenSSL Command-Line HOWTO
OpenSSL Command-Line HOWTO The openssl application that ships with the OpenSSL libraries can perform ...
- Eplan 2D安装版布局,部件、端子竖放
部件竖放,不是通过变量的选择实现,而是通过设置实现的,具体设置在: 选项-设置-用户-2D安装板布局: 部件方向-更改为 垂直 部件放置-旋转角度-更改为90° 这样在连续放置部件的时候就变为竖放了, ...
- C# 使用 SAP NCO3.0 调用SAP RFC函数接口
最近使用C#调用SAP RFC函数,SAP提供了NCO3.0组件. 下载组件安装,之后引用“sapnco.dll”和“sapnco_utils.dll”两个文件. 在程序中 using SAP.Mid ...
- 用对 gitignore
使用 git 做代码管理工具时,设置 gitignore 是必不可少的流程,一些系统或者 IDE 会在目录下生成与项目不相关的文件,而这些文件我们不期望被提交到仓库之中.理解 gitignore 的 ...
- jsp中如何整合CKEditor+CKFinder实现文件上传
最近笔者做了一个新闻发布平台,放弃了之前的FCKEditor编辑器,使用了CKEditor+CKFinder,虽然免费的CKFinder是Demo版本,但是功能完整,而且用户都是比较集中精神发新闻的人 ...
- 修改mysql默认字符集的方法
+--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...