题目来源:

  https://leetcode.com/problems/word-ladder/


题意分析:

  和上一题目类似,给定一个beginWord和一个endWord,以及一个字典list。这题需要的是要beginWord转化成endWord的最短路径长度。


题目思路:

  最短路的思路。将beginWord放进队列,如果队列不为空,那么取出第一个数,将其周围的在字典的字符放进队列,直到周围的存在endword。


代码(python):

  

 import Queue
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: Set[str]
:rtype: int
"""
q,ans = Queue.Queue(),{}
q.put(beginWord);wordList.add(endWord)
ans[beginWord] = 1
while not q.empty():
tmp = q.get()
if tmp == endWord:
return ans[endWord]
for i in range(len(tmp)):
part1,part2 = tmp[:i],tmp[i+1:]
for j in 'abcdefghijklmnopqrstuvwxyz':
if tmp[i] != j:
newword = part1 + j + part2
if newword in wordList:
q.put(newword)
ans[newword] = ans[tmp] + 1
wordList.remove(newword)
return 0

[LeetCode]题解(python):127-Word Ladder的更多相关文章

  1. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  2. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

  3. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  5. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  6. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  7. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  8. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  9. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  10. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

随机推荐

  1. UI_拖动View

    方法一 在touchesMoved中 // 获取到触摸的手指 UITouch *touch = [touches anyObject]; // 获取集合中对象 // 获取開始时的触摸点 CGPoint ...

  2. SQL练习之求解填字游戏

    SELECT * FROM dbo.spt_values

  3. validate 表单验证

    转自博客园:http://www.cnblogs.com/easyinsc/archive/2009/02/27/1407826.html (1)required:true               ...

  4. C++ this 指针

    类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...

  5. MVC定义路由

    标准路由配置 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Defa ...

  6. Centos6.5中 一键安装LNMP 安装Yii2.0 手工配置

    1.一键安装LNMP cd /usr wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz tar zxf lnmp1.-full.tar.gz ...

  7. MySQL学习系列一---命令行连接mysql和执行sql文件

    1.命令行连接mysql #mysql -h(主机) -u(用户名) -p (数据库名) mysql -hlocalhost -uroot -p testdb Enter password: **** ...

  8. discuz_style_default.xml 修改

    <?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id=" ...

  9. UPPH、UPH

    UPPH=units Per Hour Per Person,单位小时人均产能,是公司作为衡量员工工作绩效的重要指标. UPPH是衡量员工单位时间工作量的一种绩效指标. UPPH计算方式如下: UPP ...

  10. SPOJ 1812 Longest Common Substring II(后缀自动机)

    [题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...