127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
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.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
这道题是经典的广度有优先搜索的例子,也是Dijkstra's algorithm的变形。
以题目给出的例子为例,其实就是在所有路径的权重都为1的情况下求出下列无向图中从节点hit到节点cog的最短路径:

PS:图中相互之间只相差一个字母的单词都是相邻节点。
利用BFS算法,维持两个集合: visited 和 wordSet

从hit开始出发,找到唯一的相邻节点:hot, 把它放进visited中,第一次循环结束。 PS: 所谓找到相邻的节点,在题目中就是找出和该单词之相差一个字母的所有单词。请看最后代码的详细实现。

查看hot节点的所有相邻节点(已经被访问过的hit除外),找到lot和dot, 放进visited中。第二次循环结束。

找出新家进来的lot和dot的未被访问的相邻节点,分别是log和dog放进visited中。第三次循环结束。

找出log的未被访问的相邻节点cog,放进结合中。第四次循环结束。由于cog就是endWord,任务结束,跳出循环。

这里总共经历了四次循环,每循环一次,其实就是从beginWord想endWord变化的一步,因此循环的次数(加上1)就是从beginWord想endWord转变经历的 number of steps。
class Solution:
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
wordList = set(wordList)
visited = [beginWord]
visited = set(visited)
dist = 1 while endWord not in visited:
temp = set()
for word in visited:
for i in range(len(word)):
newwordL = list(word)
for ch in 'qwertyuiopasdfghjklzxcvbnm':
newwordL[i] = ch
newWord = ''.join(newwordL)
if newWord in wordList:
temp.add(newWord)
wordList.remove(newWord) dist += 1
if len(temp) == 0: # if 0, it never gets to the endWord
return 0 visited = temp return dist
参考链接:https://www.jianshu.com/p/753bd585d57e
127. 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 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- 127 Word Ladder 单词接龙
给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则: 每次只能改变一个字母. 变换过程中的 ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 127. Word Ladder(M)
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
随机推荐
- 安装wampserver时提示丢失MSVCR110.dll
安装Wampserver 2后启动的时候提示系统错误:MSVCR110.dll丢失. 在wampserver官网上有例如以下提示: 于是卸载原来的WAMPSERVER 2 ,在http://www.m ...
- iOS开发之-- 从当前隐藏导航界面push到下一个显示导航界面出现闪一下的问题
在修改项目代码的过程中,遇到一个问题,就是比如主页面的导航栏是隐藏的,但是需要push到别的页面,这个时候,会出现导航栏闪一下的情况, 下面是我写的一种方案,也就是在loadView这个生命周期函数中 ...
- Oracle自定义聚集函数
今天工作中看见别人写的自定义聚集函数,所以一门心思的想搞懂,就在网上找资料了. 自定义聚集函数 自定义聚集函数接口简介 Oracle提供了很多预定义好的聚集函数,比如Max(), Sum(), AVG ...
- 微软发布新版 Skype Linux 客户端
导读 前两天,微软说要给Linux 用户带来一个令人兴奋的新闻,今天,这个新闻来了.它刚刚为 Linux 发布了一个新的 Skype 客户端.此次发布,微软为 Linux 带来的 Skype 客户端与 ...
- 用Java向数据库中插入大量数据时的优化
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: public void ex ...
- 常问面试题:C++中sizeof的陷阱及应答
C++中sizeof是经常被问到的一个概念,比如,下面的几个关于sizeof的面试题反复出现在各大IT公司的技术面试当中,我们有必要完全理解并掌握.注:在曾经面试大公司时,我的确被问到过这样的问题. ...
- MAC OSX安装多个版本的JAVA(jdk jre通用)
MAC自带的jdk1.6是苹果公司自己修改的jdk版本,被广泛应用于各种mac软件,具有不可替代性:同时,java1.7和1.8有时也需要用到.因此,在mac上安装.使用多个版本的java具有重要意义 ...
- LeetCode-Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- 厚积薄发系列之JDBC详解
创建一个以JDBC链接数据库的程序,包含七个步骤 1.加载JDBC驱动 加载要连接的数据库的驱动到JVM 如何加载?forName(数据库驱动) MySQL:Class.forName("c ...
- 160427、CSS3实战笔记--多列布局
通过阅读和学习书籍<CSS3实战>总结 <CSS3实战>/成林著.—北京机械工业出版社2011.5 多列布局适合纯文字版式设计,如报纸内和杂志类网页布局,不适合做网页结构布 ...