【leetcode】126. Word Ladder II
题目如下:

解题思路: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的更多相关文章
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- 【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 ...
- 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)
原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...
- 【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 ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 【LeetCode】127. Word Ladder 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
随机推荐
- postman 简单使用教程
Postman 安装 Postman 接口测试(Collection) Postman 接口测试(测试用例)Postman 接口测试(变量与参数化)Postman 接口测试(非 UI 运行模式 ...
- 给网页标题添加icon小图标
so easy: 给网页标题添加icon小图标: 直接在html文件的head部分添加下面代码(注意href的路径): <link rel="icon" type=" ...
- cron 定时任两种配置方式
第一种:xml文件方式 <bean id="commonTimer" class="com.course.wx.timer.CommonTimer"> ...
- 【C#学习笔记】string.Format对C#字符串格式化
文章转自:CSDN http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格 ...
- Decision Tree Algorithm
Decision Tree算法的思路是,将原始问题不断递归地细分为子问题,直到子问题直接可获得答案为止.在模型训练的过程中,根据训练集去做树的生长(Grow the tree),生长所有可能的Bran ...
- Hexo-博客yilia主题创建分类(categories)和标签(tags)首页
转载自:http://orzcss.com/posts/5a207d64/ 概述 默认安装的 hexo 本身是没有分类和标签首页的,例如:http://orzcss.com/categories/页面 ...
- sql片段的定义
<!-- sql片段 id 表示唯一标示 这里不加where是因为 sql片段只对单表查询才抽取出来 这样的重用性更高 --> <sql id="query_user_wh ...
- java 日期工具
package com.neuxa.is.workflow.utils; import java.sql.Timestamp;import java.text.DateFormat;import ja ...
- .net core 调用webservice同步方法
更新VS2019 16.1版本 支持WebService同步调用 在连接服务中->选择客户端选项->Generate Synchronout Operations选择划勾 生成同步操作 ...
- Linux笔记1-简介
以下为观看兄弟连视频教程所记. 1.简介 1965年,MIT.GE.AT&T的贝尔实验室 --> Multics 分时.庞大 1969年,贝尔实验室的肯·汤普森 --> Unix ...