题目如下:

解题思路: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. React Hook:使用 useEffect

    React Hook:使用 useEffect 一.描述 二.需要清理的副作用 1.在 class 组件中 2.使用 effect Hook 的示例 1.useEffect 做了什么? 2.为什么在组 ...

  2. 如何创建自定义的Resource实例

    由Resource的构造函数Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)了解到,需要获取ap ...

  3. p5342 [TJOI2019]甲苯先生的线段树

    分析  代码 #include<bits/stdc++.h> using namespace std; #define int long long ],yy[],cnt1,cnt2; ][ ...

  4. SQL element_at函数

    库里有类似josn形式的字符串数据attr{"a":"123","b":"234"."c":&quo ...

  5. cannot be resolved to a type 错误解决方法

    引言:  eclipse新导入的项目经常可以看到“XX cannot be resolved to a type”的报错信息.本文将做以简单总结. 正文:      (1)jdk不匹配(或不存在)   ...

  6. 部署 H3C CAS E0306

    目录 目录 前文列表 H3C CAS CVK Cloud Virtualization Kernel 虚拟化内核平台 CVMCloud Virtualization Manager 虚拟化管理系统 C ...

  7. js面向对象程序设计之构造函数

    再上一篇的开头说了创建对象的两种方式,一种是Object构造函数的方式,一种是对象字面量的方法.但这些方式创建多个对象的时候都会产生大量的重复代码.经过技术的进步也演化出来许多的创建对象的模式.本章会 ...

  8. vue同意本站协议的制作

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

  9. IDEA 光标显示注释

  10. 基于dvwa环境下级别为low的SQL手工注入教程

    基于dvwa环境下级别为low的SQL手工注入教程: 首先是进入已搭建好的dvwa环境中去(一定要搭建好dvwa环境才能进行下面的操作),这可能会是一些初学者所面临的的第一个问题,比如我,曾为了寻找这 ...