leetcode笔记: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.
二. 题目分析
參考链接:http://www.mamicode.com/info-detail-448603.html
能够将这道题看成是一个图的问题。我们将题目映射到图中,顶点是每个字符串,然后两个字符串假设相差一个字符则进行连边。
我们的字符集仅仅有小写字母。并且字符串的长度固定,假设是L。那么能够注意到每个字符能够相应的边有25个(26个小写字母去掉自己)。那么一个字符串可能存在的边是25*L条。接下来就是检查这些相应的字符串是否在字典内。就能够得到一个完整的图的结构。
依据题目要求,等价于求这个图中一个顶点到还有一个顶点的最短路径。我们一般用BFS广度优先。
这道题。我们仅仅能用最简单的办法去做,每次改变单词的一个字母。然后逐渐搜索。这种求最短路径,树最小深度问题用BFS最合适。
和当前单词相邻的单词,就是和顶点共边的还有一个顶点。是对当前单词改变一个字母且在字典内存在的单词。
找到一个单词的相邻单词,增加BFS队列后。我们要从字典内删除。由于不删除会造成相似hog->hot->hog这种死循环。并且删除对求最短路径没有影响,由于我们第一次找到的单词肯定是最短路径。我们是层序遍历去搜索的,最早找到的一定是最短路径。即使后面的其它单词也能转换成它。路径肯定不会比当前的路径短。
这道题仅要求求出最短路径长度,不须要求输出最短路径,所以能够删除这个单词。
BFS队列之间用空串”“来标示层与层的间隔,每次碰到层的结尾,遍历深度+1。进入下一层。
三. 演示样例代码
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(start.size() == 0 || end.size() == 0) return 0;
queue<string> wordQ;
wordQ.push(start);
wordQ.push("");
int path = 1;
while(!wordQ.empty())
{
string str = wordQ.front();
wordQ.pop();
if(str != "")
{
int len = str.size();
for(int i = 0; i < len; i++)
{
char tmp = str[i];
for(char c = 'a'; c <= 'z'; c++)
{
if(c == tmp) continue;
str[i] = c;
if(str == end) return path + 1; //假设改变后的单词等于end 返回path+1
if(dict.find(str) != dict.end())
{
wordQ.push(str);
dict.erase(str); //字典内删除这个词 防止重复走
}
}
str[i] = tmp; //重置回原来的单词
}
}
else if(!wordQ.empty())
{
//到达当前层的结尾。并且不是最后一层的结尾
path++;
wordQ.push("");
}
}
return 0;
}
};
leetcode笔记:Word Ladder的更多相关文章
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- [Leetcode Week5]Word Ladder
Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- 2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)
Description Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob ta ...
- [bzoj4602][Sdoi2016]齿轮——dfs
题目 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v的齿轮会转动y圈.传 ...
- iOS float小数四舍五入
http://blog.csdn.net/fanjunxi1990/article/details/21536189 直接贴代码了 #import "ViewController.h&quo ...
- python基础===pip安装模块失败
此情况只用于网络不畅的安装模块背景: 总出现红色的 Could not find a version that satisfies the requirement pymongo(from versi ...
- (二十八)fopen与读写的标识r,r+,rb+,rt+,w+.....
fopen与读写的标识r,r+,rb+,rt+,w+..... 函数简介 函数功能: 打开一个文件 函数原型:FILE * fopen(const char * path,const char * m ...
- vue + webpack + gulp 简单环境 搭建
一.物料准备 废话不多说,直接上 package.json { "name": "vwp", "version": "1.0.0& ...
- 用IJ和gradle启动elasticsearch5.4.3
环境准备 jdk gradle3.3+ idea git 从git clone源码 git checkout v5.4.3 打开项目 1. 在edit configurations添加new conf ...
- elasticsearch SpanNearQuery inOrder参数
一直没有注意还有一个inOrder参数: public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) When inOrd ...
- JSONObject常用的API
http://www.cnblogs.com/java-pan/archive/2012/04/07/jsonobject.html 1.介绍基于JSONObject 1.1的API 2.只介绍常用的 ...
- C#汉字转十六进制
public class chsHex{/// <summary>/// 从汉字转换到16进制/// </summary>/// <param name="s& ...