[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 length5.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
题意:从一个词变为另一个词,每次只能变化一个字符,变化的中间词都在字典中,求最短的路径。
思路:图论中单源最短路,求单源最短路比较通用算法是BFS和Dijkstra,其区别在于BFS不能用于带权重图,而Dijkstra可以。另外,BFS和Dijkstra的区别是前者的时间复杂度是O(n),后者最多优化到O(mlogn),如果条件成立一般选择BFS。本题中,两字符串之间是无权重的,连通是1,不连通是无穷,故本题采用BFS方法。
首先,要进行图的映射,顶点是每个字符串,若是两个字符串相差1个字符且后者在dict中,就相连构成边;然后,将起点加入到队列中,遍历和其相差为1的字符串,将其加入到队列中,直到找到目标字符串(找不到就返回0)。这里值得注意的是:寻找和某一个字符相差1的字符串的方式,不是遍历字典中的每一个字符串,这样当字典中元素较多时,将会耗时严重。这次采用,对该字符串的每个位置上的字符,用26个字母进行替换,找到和其相差为1的所有字符串;还有一点值得注意的是,每次在字典中找到某一个字符串以后,要在字典中删除该字符串,这样可以避免重复查找(详细说明见这里)。以题中的例子为例,见下图:

如上图,每一层依次加入到队列中,只要先找到目标词语,就返回此时的值就行。参考了这里,代码如下:
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict)
{
queue<pair<string,int>> que;
que.push(make_pair(start,));
dict.erase(dict.find(start));
while( !que.empty())
{
auto val=que.front();
que.pop();
if(val.first==end) return val.second;
for(int i=;i<val.first.size();++i)
{
string str=val.first;
for(int j=;j<;++j)
{
str[i]='a'+j;
if(dict.find(str) !=dict.end())
{
que.push(make_pair(str,val.second+));
dict.erase(str);
}
}
}
}
return ;
}
};
思路是一样的,换一种写法;
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict)
{
unordered_map<string,int> m;
queue<string> que;
m[start]=;
que.push(start);
while( !que.empty())
{
string val=que.front();
que.pop();
for(int i=;i<val.size();++i)
{
string str=val;
for(int j=;j<;++j)
{
str[i]='a'+j;
if(dict.count(str)&&str==end)
return m[val]+;
if(dict.count(str)&& !m.count(str))
{
que.push(str);
m[str]=m[val]+;
}
}
}
}
return ;
}
};
[Leetcode] word ladder 单词阶梯的更多相关文章
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
随机推荐
- 微信小程序引用iconfont图标字体解决方案;
1)首先,登录阿里巴巴iconfont.cn 2)新建项目 3)点击icon收藏 4)加入到test项目中 5)下载到本地解压 6)生成代码 7)复制iconfont.css到xxx.wx ...
- 介绍三种PHP加密解密算法
PHP加密解密算法 这里主要介绍三种常用的加密解密算法:方法一: /** * @param $string 要加密/解密的字符串 * @param string $operation 类型,ENCOD ...
- 【Thrift一】Thrift安装部署
Thrift安装部署 Thrift安装部署 下载源码包 安装g++ 解压Thrift安装包 安装boost开发工具 测试(python版) 下载源码包 wget http://apache.fayea ...
- C语言实现简易扫雷
首先,写代码之前要将整体思路写出来: 扫雷游戏:1.需要两个二维数组,一个用来展示,一个用来放雷; 2.整体骨架在代码中都有注释说明; 3.游戏难度比较简单,适合初学者观看,如果有大佬看明白,可以指点 ...
- ffmpeg使用笔记
1.从mp4中提取h264:ffmpeg -i 264.mp4 -codec copy -bsf h264_mp4toannexb -f h264 output.h2642.从mp4中提取hevc:f ...
- FIFO队列(First In First Out)和优先队列
queue<类型名> q; q.size() - 返回队列中元素个数 q.empty() - 若队列为空,返回true ,否则返回false q.pop() - 删除队首元素,但不返回其值 ...
- maven中settings文件的配置
鉴于上一个博客写的maven打包,读者老爷们可能找不到settings文件的配置,这里专门附上settings文件的配置: 有的settings文件只含有一些空标签,需要手动去配置. <?xml ...
- SAPの販売管理で、価格設定をするまでの関連カスタマイズ画面
この記事ではSAP SDで.価格を決めるまでに必要な設定画面について述べています. condition table (条件テーブル) 条件レコードのキー項目を定義したもの.3桁の数字で名前がついている ...
- Java常考面试题
Java常考面试题 1. 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 答:Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行 ...
- 保证IO流不出错
package com.io.demo1; import java.io.FileInputStream;import java.io.IOException; /** * 测试IO * io流,输入 ...