题目来源:

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


题意分析:

  给定一个beginWord和一个endWord,以及一个字典单词,找出所有从beginWord变成endWord的所有最短路径,单词变化每次只能变一个字母,所变的字母必须在字典里面。


题目思路:

  这是一个最短路径问题。首先将beginWord放进队列,当队列不为空的时候,pop出第一个数,将它周围的在字典的push进队列。要注意的是将字段的数push进队列的同时,将其路径用dict记录下来。


代码(python):

  

 class Solution(object):
def findLadders(self, beginWord, endWord, wordlist):
"""
:type beginWord: str
:type endWord: str
:type wordlist: Set[str]
:rtype: List[List[int]]
"""
ans,q = {},[]
q.append(beginWord)
ans[beginWord] = [[beginWord]]
ans[endWord] = []
while len(q) != 0:
tmp = q.pop(0)
for i in range(len(beginWord)):
part1,part2 = tmp[:i],tmp[i + 1:]
for j in "abcdefghijklmnopqrstuvwxyz":
if tmp[i] != j:
newword = part1 + j + part2
if newword == endWord:
for k in ans[tmp]:
ans[endWord].append(k + [endWord])
while len(q) != 0:
tmp1 = q.pop(0)
if len(ans[tmp1][0]) >= len(ans[endWord][0]):
break
for ni in range(len(beginWord)):
npart1,npart2 = tmp1[:ni],tmp1[ni+1:]
for nj in "abcdefghijklmnopqrstuvwxyz":
if tmp1[ni] != nj:
nw = npart1 + nj + npart2
if endWord == nw:
for nk in ans[tmp1]:
ans[endWord].append(nk + [endWord])
break
if newword in wordlist:
q.append(newword)
ans[newword] = []
for k in ans[tmp]:
ans[newword].append(k + [newword])
wordlist.remove(newword)
elif newword in ans and len(ans[newword][0]) == len(ans[tmp][0]) + 1:
for k in ans[tmp]:
ans[newword].append(k + [newword])
return ans[endWord]

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

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

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

  2. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  3. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  4. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  5. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

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

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  7. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  8. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  9. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  10. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

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

随机推荐

  1. iOS实现文件上传功能模块

    iOS实现文件上传功能,首先要知道的是,上传到服务器的数据格式,一般采用HTTP文件上传协议.如下图 如图所示,只要设置好了HTTP的协议格式,就可以实现文件上传功能. 代码如下: //图片上传模块 ...

  2. Pig Latin儿童黑话(java)

     ●假设单词以辅音字母開始,将词首的辅音字母字符串(第一个元音字母前的全部字母)从单词的开头移动到末尾,然后加上后缀ay,这样就形成了它的pig  latin. ●假设单词以元音字母開始,仅仅须要 ...

  3. 从零开始学习UNITY3D(GUI篇 GUI.Window)

    unity3d里面,也是包含window窗体的,下面看一下GUI.Window方法的详情 下面我们用代码实现一个通过开关显示窗体的隐藏和显示的功能,代码如下: public class windows ...

  4. cron 定时任务

    cron 是linux下的定时任务: M H D m d cmd.  这是一种cron文件格式.   M: 分钟(0-59). H:小时(0-23). D:天(1-31). m: 月(1-12). d ...

  5. ASP.NET LINQ SQL执行超时的问题

    最近在写一个航材取价的程序.由于执行的语句复杂,数据量比较大,容易造成超时. 看提示应该是执行SQL的时候超时,我在程序中用的linq. 实际把SQL语句单独执行的时候观察也要40秒左右. 查资料得知 ...

  6. js简单实现链式调用

    链式调用实现原理:对象中的方法执行后返回对象自身即可以实现链式操作.说白了就是每一次调用方法返回的是同一个对象才可以链式调用. js简单实现链式调用demo Object.prototype.show ...

  7. leetcode Roman to Integer python

    class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int "& ...

  8. 《JavaScript+DOM编程艺术》的摘要(五)-----添加insertAfter

    在JS原生里面,没有提供insertAfter这个方法,不过我们可以利用appendChild.insertBefore.parentNode这些方法创建一个insertAfter方法,代码如下: f ...

  9. 原生js倒计时和显示当前时间

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. python parse命令行参数

    #!/usr/bin/env python import sys def main(argv): for arg in argv: print arg if __name__ == '__main__ ...