题目如下:

解题思路:DFS或者BFS都行。本题的关键在于减少重复计算。我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表;另外是用dp数组记录从startword开始到wordlist每一个word的最小转换次数,这一点非常重要,可以过滤很多无效的运算。

代码如下:

class Solution(object):
def getLadderList(self, w,d):
l = []
r = []
for i in xrange(26):
l.append(chr(i + ord('a')))
for i in range(len(w)):
for j in l:
if w[i] != j:
v = w[:i] + j + w[i+1:]
if v in d:
r.append(v)
return r def findLadders(self, beginWord, endWord, wordList):
#print len(wordList)
dic = {}
for i,v in enumerate(wordList):
dic[v] = i if endWord not in dic:
return [] queue = [(beginWord,0,beginWord)]
res = []
shortest = len(wordList) + 1
dp = [shortest for i in wordList] dic_ladderlist = {}
while len(queue) > 0:
word,count,path = queue.pop(0)
if count > shortest:
continue
if word in dic_ladderlist:
wl = dic_ladderlist[word]
else:
wl = self.getLadderList(word,dic)
dic_ladderlist[word] = wl for i in wl:
if dp[dic[i]] >= count+1 :
if i == endWord:
#path = path + ' ' + i
pl = path.split(' ')
pl.append(endWord)
if len(pl) < shortest:
res = []
res.append(pl)
shortest = len(pl)
elif len(pl) == shortest:
res.append(pl)
shortest = len(pl)
continue
queue.append((i,count + 1,path + ' ' + i))
dp[dic[i]] = count + 1
return res

【leetcode】126. Word Ladder II的更多相关文章

  1. 【LeetCode】127. Word Ladder

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

  2. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  4. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

  5. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  6. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

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

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

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

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

  9. 126. Word Ladder II(hard)

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

随机推荐

  1. [CSP-S模拟测试]:传递(暴力+bitset)

    题目描述 我们称一个有向图$G$是传递的,当且仅当对于图$G$的三个不同顶点$a,b,c$,若图$G$中有一条边从$a$到$b$且有一条边从$b$到$c$,那么图中也有一条边从$a$到$c$.我们称一 ...

  2. json和list转换

    1.json转list List<TenantMember> tm= (List<TenantMember>)JSONArray.toCollection(JSONArray. ...

  3. php面试专题---mysql数据库分库分表

    php面试专题---mysql数据库分库分表 一.总结 一句话总结: 通过数据切分技术将一个大的MySQLServer切分成多个小的MySQLServer,既攻克了写入性能瓶颈问题,同一时候也再一次提 ...

  4. linux设备驱动第四篇:从如何定位oops的代码行谈驱动调试方法

    上一篇我们大概聊了如何写一个简单的字符设备驱动,我们不是神,写代码肯定会出现问题,我们需要在编写代码的过程中不断调试.在普通的c应用程序中,我们经常使用printf来输出信息,或者使用gdb来调试程序 ...

  5. inline-block的间隙问题 box-orient属性 line-clamp属性 margin问题

    只要设了 display:inline-block 将元素变成行级块元素的时候,会自带空隙,即使你设了 margin 和 padding 依然没有效果! 解决办法:只要在父元素上加上font-size ...

  6. nginx配置多个虚拟主机(mac)

    1 . 安装  通过homebrew安装nginx,默认安装在:/usr/local/Cellar/nginx/版本号.配置文件在路径:/usr/local/etc/nginx ,默认配置文件ngin ...

  7. win10 文件管理器频繁卡死

    参考: https://www.xitmi.com/1589.html

  8. CET-6 分频周计划生词筛选(Week 3)

    点我阅读 Week 3 2016.09.11 p113 manipulate + propel p114 expedition + deficit p115 all p116 envisage p11 ...

  9. Mac007--Mysq服务端&客户端安装

    一.安装Mysql服务端与Navicat Premium客户端 参见博客:https://blog.csdn.net/wtdask/article/details/79025674 安装mysql服务 ...

  10. flex布局解说和属性

    1. flex-direction 规定当前DIV下面的子元素是横向布局还是纵向布局 row 默认值,横向布局相当于float:left column 纵向,相当于DIV默认的垂直方向 2.justi ...