【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 ...
随机推荐
- 5 October
POJ2676 Sudoku 位运算 + 搜索.更好的优化方法:方案数最小的空格先填. 把某一位 置为 0:a &=~ (1<<n) 把某一位 置为 1:a |= (1<&l ...
- jQuery插件3种类型
1.封装对象方法的插件 这种插件是将对象方法封装起来,用于对通过选择器获取的jQuery对象进行操作,是最常见的一种插件. 此类插件可以发挥出jQuery选择器的强大优势,有相当一部分的jQuery方 ...
- python web自动化测试框架搭建(功能&接口)——接口用例实现
测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...
- Convolutional Neural Networks(2):Sparse Interactions, Receptive Field and Parameter Sharing
Sparse Interactions, Receptive Field and Parameter Sharing是整个CNN深度网络的核心部分,我们用本文来具体分析其原理. 首先我们考虑Feedf ...
- (转载)如何在 Github 上发现优秀的开源项目?
转载自:传送门 之前发过一系列有关 GitHub 的文章,有同学问了,GitHub 我大概了解了,Git 也差不多会使用了,但是还是搞不清 GitHub 如何帮助我的工作,怎么提升我的工作效率? 问到 ...
- Mac004--Tomcat安装
Mac--Tomcat安装 一.Tomcat下载 https://tomcat.apache.org/download-80.cgi 下载Tomcat注意tomcat与jdk版本要一致.jdk1.8版 ...
- Learning OSG programing---osgAnimation(2)
osg::Node* createBase(const osg::Vec3& center,float radius) { ; ; *radius; *radius; osg::Vec3 v0 ...
- 如何在Python中使用Linux epoll
如何在Python中使用Linux epoll 内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码 介绍 从2.6版开始,Pyt ...
- phpcms列表分页ajax加载更多
1.在phpcms\modules\content\index.php文件中添加以下函数: /*列表分页ajax加载更多*/ public function homeajaxlist() { if( ...
- CSS动画划入划出酷炫
HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...