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 ...
随机推荐
- openstack 问题澄清
1. neutron中plugin与agent是一一对应的吗? 在不使用ml2时,plugin与agent一一对应,如ovs-plugin与ovs-agent:当使用ml2 plugin时,该plug ...
- mysql七:数据备份、pymysql模块
阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- C++类学习
一.C++类的定义 C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性 protected: //行为或属性 private: / ...
- python学记笔记 2 异步IO
在IO编程中,我们知道CPU的速度远远快于磁盘,网络IO,在一个线程中,CPU执行速度的代码非常快,然而遇到IO操作就需要阻塞 需要等待IO操作完成才能继续下一步的动作.这种情况叫做同步IO 在IO操 ...
- 【bzoj4636】蒟蒻的数列
由于数据范围过大,直接线段树会炸,离散化或者动态开点都行. 打个标记在树上,最后把树dfs一边算一下即可. #include<bits/stdc++.h> #define N 100000 ...
- magento smtp设置
我安装的版本是ASchroder_SMTPPro-2.0.6.tgz 然后测试 但是没成功,会有报错,报错: 提示没有默认模板,原因是该插件没有带模板,所有会有这样的提示.
- django CXRF介绍
CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...
- UVA 10340 All in All(字符串,朴素匹配)
#include <stdio.h> #include <algorithm> #include <cstring> using namespace std; ], ...
- Problem A: 英雄无敌3(1)【dp/待补】
Problem A: 英雄无敌3(1) Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 16[Submit][Status][ ...
- Superbull(最大生成树)(Kruskal)
Superbull 时间限制: 1 Sec 内存限制: 64 MB提交: 49 解决: 13[提交][状态][讨论版] 题目描述 Bessie and her friends are playin ...