[leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python
原题地址:http://oj.leetcode.com/problems/word-ladder-ii/
参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405
http://chaoren.is-programmer.com/
题意:给定start单词,end单词,以及一个dict字典。要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict中,且每次改变一个字母。如start="hit"; end="cog"; dict={"hot","dot","dog","lot","log"},则答案为:[["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]。这是leetcode oj给的例子。我在实现的时候发现这个例子有点问题:end单词不在dict中。实际的测试用例应该是start和end单词都在dict中的,因为如果提前做一个删除start或者end单词的操作的话,就通不过了。我用正确的程序去测试oj给的这个例子也无法通过,就姑且认为start单词和end单词都在dict中吧,这样写出来的程序才能通过。
Word Ladder II这一道题还是比较难的,是leetcode oj中通过率最低的一道题。而由于我一直在用python来刷题,且一直在网上找不到用python写的解法,自己又写不出来,所以参考了网上的C++解法以及kitt的python程序,在此表示感谢。
解题关键点:1,这里的dict是python中的set()类型。
2,使用前驱单词表prevMap,是python中的字典类型。用来记录单词的前驱。比如prevMap={cog:[log, dog]}表示cog的前驱是:log和dog。
3,在word ladder这道题中我们使用了队列,在word ladder ii中也需要使用队列,只不过在这个程序中使用了两个set()来模拟队列。candidates[previous]中存储的是前面一层的单词。如{dot,lot}为第三层单词。在程序开始执行时,现将前面一层的单词即candidates[previous]中的单词在dict中删除,再将candidates[current]清空,然后根据candidates[previous]中的单词寻找下一层的单词,如{dot,lot}的下一层为{dog,log},并将{dog,log}存入candidates[current]中,同时将单词存入前驱单词表中。下一次循环开始时,上一次循环的candidates[current]变成了candidates[previous],而上一次循环的candidates[previous]变成了candidates[current]并清空。如此反复执行,当某一次循环中的candidates[current]中出现了end单词时,说明我们的路径已经找出来了,工作完成了。
4,程序中使用了一个子函数buildpath来重建每一条路径。如oj给的例子所产生的prevMap={cog:[log,dog], log:[lot], dog:[dot], dot:[hot], lot:[hot], hot:[hit]},这个prevMap可以使用DFS来重建每一条路径。
源代码:
class Solution:
# @param start, a string
# @param end, a string
# @param dict, a set of string
# @return a list of lists of string
def findLadders(self, start, end, dict):
def buildpath(path, word):
if len(prevMap[word])==0:
path.append(word); currPath=path[:]
currPath.reverse(); result.append(currPath)
path.pop();
return
path.append(word)
for iter in prevMap[word]:
buildpath(path, iter)
path.pop() result=[]
prevMap={}
length=len(start)
for i in dict:
prevMap[i]=[]
candidates=[set(),set()]; current=0; previous=1
candidates[current].add(start)
while True:
current, previous=previous, current
for i in candidates[previous]: dict.remove(i)
candidates[current].clear()
for word in candidates[previous]:
for i in range(length):
part1=word[:i]; part2=word[i+1:]
for j in 'abcdefghijklmnopqrstuvwxyz':
if word[i]!=j:
nextword=part1+j+part2
if nextword in dict:
prevMap[nextword].append(word)
candidates[current].add(nextword)
if len(candidates[current])==0: return result
if end in candidates[current]: break
buildpath([], end)
return result
[leetcode]Word Ladder II @ Python的更多相关文章
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- leetcode—word ladder II
1.题目描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence( ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
随机推荐
- View 事件分发
View 事件分发 学习自 <Android开发艺术探索> 官方文档-MotionEvent 事件分发机制漫谈 View的事件分发机制,使我们了解View的工作原理继而学习如何自定义Vie ...
- leetcode 二叉搜索树中第K小的元素 python
二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...
- jvm 参数配置优化
abtest业务线上接口每分钟执行3万多次,到半夜1点多访问量小的时候会经常报内存占比过高, 经调研分析发现与白天访问量高时线程数.cpu等无太大差异,主要差异存在内存会涨到很高并持续 一段时间. 现 ...
- P4812 D’s problem(d)
P4812 D’s problem(d)From: admin 时间: 1000ms / 空间: 65536KiB / Java类名: Main 背景 NOIP春季系列课程 描述 小D是一名魔法师,它 ...
- HDU5320 : Fan Li
考虑枚举左端点i,则随着右端点的右移,一共只有$O(\log n)$种不同的gcd取值.所以首先通过ST表+二分查找预处理出$O(n\log n)$个四元组(x,i,l,r),表示左端点为i,右端点取 ...
- 国外IOS UI指南
国外IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻译) ...
- TCP三次握手&四次挥手(示意图)
经典的三次握手示意图:(#add,“握手”即图中左边到右边的连线) 经典的四次握手关闭图: TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即t ...
- Android的设计尺寸
术语和概念 屏幕尺寸 指实际的物理尺寸,为屏幕对角线的测量.为了简单起见,Android把实际屏幕尺寸分为四个广义的大小:小,正常,大,特大. 像素(PX) 代表屏幕上一个物理的像素点代表屏幕上一个物 ...
- Android 应用程序之间内容分享详解(一)
一个Andoird应用程序的重要的地方是他们有相互沟通和整合的能力,一个应用程序可以和另一个应用程序交互,接下来我们来看看Android应用之间的内容分享 当你构建Intent的时候,必须要指定Int ...
- LINUX IO 图解
http://blog.chinaunix.net/uid/29075379/cid-183430-list-1.html