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 ...
随机推荐
- 【洛谷 SP2878】Knights of the Round Table(双联通分量)
先放这吧,没时间写,明天再补 "明天到了" 题目链接 题意:求不在任何奇环内的点的数量. Tarjan求点双联通分量,然后再染色判断是不是二分图就好了. 只是不懂为什么Tarjan ...
- 【BZOJ 3907】网格(Catalan数)
题目链接 这个题推导公式跟\(Catalan\)数是一样的,可得解为\(C_{n+m}^n-C_{n+m}^{n+1}\) 然后套组合数公式\(C_n^m=\frac{n!}{m!(n-m)!}\) ...
- spring自定义参数绑定(日期格式转换)
spring参数绑定时可能出现 BindException(参数绑定异常),类似下面的日期绑定异常(前台传过来是String类型,实际的pojo是Date类型) default message [Fa ...
- POJ2479(最长连续子序列和)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37035 Accepted: 11551 Des ...
- 一次向svn中增加所有新增文件 svn add all new files【转】
以下摘自:<卓有成效的程序员>之自动化 转自:http://blog.csdn.net/spare_h/article/details/6677435 我经常会一次往Subversion里 ...
- easyui获取当前选中的tabs
$("#" + $("#tabs").find("iframe")[$(".tabs-header ul li").in ...
- Ansi,UTF8,Unicode,ASCII编码的区别 ---我看完了 明白了很多
来自:http://blog.csdn.net/xiongxiao/article/details/3741731 ------------------------------------------ ...
- hdu 1068(最大独立集)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- c语言,warning: return type of 'main' is not `int'怎么解决?
////警告可以忽略,但如果严格点的话 #include<stdio.h> #include<math.h> int main(int argc, char *arg[]) ...
- Java基础:GC机制
上一节,简单的介绍了java当中的内存模型,那么经常和内存模型一起提到的JAVA垃圾回收机制当然也需要在这里一并的总结一下. 所谓是垃圾回收机制,用通俗的话来说,就是将那些没有被任何变量引用的实例对象 ...