题目如下:

解题思路: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. 为 PhpStorm 配置 Xdebug 来调试代码

    当项目越来越复杂,排错就越发困难. 你以为代码是这么运行的,但就是有未想到的功能导致流程变得不可捉摸. 此时我们需要调试啊调试... PhpStorm 是一款优秀的 PHP IDE,排除其 Java ...

  2. 初步学习jQuery之事件

    事件 页面加载 在DOM中提供了load事件用于页面加载完毕之后执行机制,jQuery提供了ready()方法实现相似的功能,但是存在以下的区别.1.DOM中的load事件没有任何的简写形式,但是在j ...

  3. scrapy xpath xpath('---').xpath('string(.)') 提取子元素全部文本

    product.xpath("div//div[@class='a-row a-spacing-mini'][1]/div[2]").xpath('string(.)')

  4. nginxUbuntu安装Nginx和正确卸载Nginx Nginx相关 与Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法

    https://www.cnblogs.com/zhaoyingjie/p/6840616.html https://blog.csdn.net/adley_app/article/details/7 ...

  5. Oralce-资源配置PROFILE

    profile:作为用户配置文件,它是密码限制,资源限制的命名集合 在安装数据库时,Oracle自动会建立名为default的默认配置文件 使用profile文件时,要注意以下几点: 建立用户时,如果 ...

  6. WebApi系列~基于RESTful标准的Web Api 转载 https://www.cnblogs.com/lori/p/3555737.html

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  7. Nginx 转写功能和Apache .htaccess 对应

    之前一直使用apache 服务器,现有一项目想转到Nginx 服务器运行.. 发现Apache 的撰写功能和 Nginx的不一样.无法通用.hataccess 文件 查阅网上资料,nginx 配置转写 ...

  8. v-text、v-html、v-bind、v-show

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. IDEA activate-power-mode插件

    一 下载activate-power-mode插件 方法1:插件库下载: 地址:http://plugins.jetbrains.com/plugin/8330-activate-power-mode ...

  10. 校内模拟赛 : Rima —— 字典树+树形DP

    首先说一下,对一个刚学Trie树的蒟蒻来说(就是我),这道题是一道好题.Trie树比较简单,所以就不详细写了. Rima 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传 ...