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 ...
随机推荐
- Filter之——GZIP全站压缩
GZIP压缩:将压缩后的文本文件,发送给浏览器,减少流量. 一.进行gzip压缩条件: 1.请求头:Accept-Encoding : gzip 告诉服务器,该浏览器支持gzip压缩. 2.响应头: ...
- SQL语句 - 数据操作
表中数据的变化牵一发而动全身,会同时导致到索引中数据的变化.因此如果查询语句不需要索引,就应该删除无用的索引以提高效率. 一.INSERT语句 1.基本插入语句 insert用于向表中输入数据,其具体 ...
- HTML5新特性之WebSocket
1.概述 HTTP协议是一种无状态协议,服务端本身不具有识别客户端的能力,必须借助外部机制,比如session和cookie,才能与特定客户端保持对话.这多多少少带来一些不便,尤其在服务器端与客户端需 ...
- 架构模式之REST架构
直至今日,分布式系统(Distributed System)已经取得了大规模的应用,特别是Web的发展,已经给软件开发带来了翻天覆地的变化,这一点已经毋庸置疑了. 构建分布式系统常用的技术通常就是使用 ...
- 说说lambda表达式与表达式树(未完)
Lambda表达式可以转换成为代码(委托)或者数据(表达式树).若将其赋值给委托,则Lambda表达式将转换为IL代码:如果赋值给 Expression<TDelegate>,则构造出一颗 ...
- Backbone1.0.0数据验证的变化
0.5.3版本对Model数据验证时,绑定Error就可以了: (function(){ var Model = Backbone.Model.extend({ initialize : functi ...
- 对依赖倒置原则(DIP)及Ioc、DI、Ioc容器的一些理解
1.概述 所谓依赖倒置原则(Dependence Inversion Principle)就是要依赖于抽象,不要依赖于具体.简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模 ...
- 无插件纯web 3D机房 (第四季:大型园区、地球仪效果和其他扩展应用)
前言 初次见面的朋友们大家好,这篇文章是"无插件纯web 3D机房"系列的第四季,感兴趣的朋友可从头开始观看,以下是正确的阅读顺序: 无插件纯web 3D机房(第一季:从零开始搭建 ...
- Java 停止一个 Thread
boolean flag=true; public void run(){ while(flag){ ... ...
- 使用log4net
原文:<使用log4net,没有日志文件生成> Posted on 2014/06/12 ================================================= ...