[LeetCode]题解(python):126-Word Ladder II
题目来源:
https://leetcode.com/problems/word-ladder-ii/
题意分析:
给定一个beginWord和一个endWord,以及一个字典单词,找出所有从beginWord变成endWord的所有最短路径,单词变化每次只能变一个字母,所变的字母必须在字典里面。
题目思路:
这是一个最短路径问题。首先将beginWord放进队列,当队列不为空的时候,pop出第一个数,将它周围的在字典的push进队列。要注意的是将字段的数push进队列的同时,将其路径用dict记录下来。
代码(python):
class Solution(object):
def findLadders(self, beginWord, endWord, wordlist):
"""
:type beginWord: str
:type endWord: str
:type wordlist: Set[str]
:rtype: List[List[int]]
"""
ans,q = {},[]
q.append(beginWord)
ans[beginWord] = [[beginWord]]
ans[endWord] = []
while len(q) != 0:
tmp = q.pop(0)
for i in range(len(beginWord)):
part1,part2 = tmp[:i],tmp[i + 1:]
for j in "abcdefghijklmnopqrstuvwxyz":
if tmp[i] != j:
newword = part1 + j + part2
if newword == endWord:
for k in ans[tmp]:
ans[endWord].append(k + [endWord])
while len(q) != 0:
tmp1 = q.pop(0)
if len(ans[tmp1][0]) >= len(ans[endWord][0]):
break
for ni in range(len(beginWord)):
npart1,npart2 = tmp1[:ni],tmp1[ni+1:]
for nj in "abcdefghijklmnopqrstuvwxyz":
if tmp1[ni] != nj:
nw = npart1 + nj + npart2
if endWord == nw:
for nk in ans[tmp1]:
ans[endWord].append(nk + [endWord])
break
if newword in wordlist:
q.append(newword)
ans[newword] = []
for k in ans[tmp]:
ans[newword].append(k + [newword])
wordlist.remove(newword)
elif newword in ans and len(ans[newword][0]) == len(ans[tmp][0]) + 1:
for k in ans[tmp]:
ans[newword].append(k + [newword])
return ans[endWord]
[LeetCode]题解(python):126-Word Ladder II的更多相关文章
- 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 ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Leetcode#126 Word Ladder II
原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...
- leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...
随机推荐
- k8s 集群基本概念
一.概述: kubernetes是google开源的容器集群管理系统,提供应用部署.维护.扩展机制等功能,利用kubernetes能方便管理跨集群运行容器化的应用,简称:k8s(k与s之间有8个字母) ...
- HTML系列(五):超链接
<a> 标签定义超链接,最重要的属性是 href 属性,它指示链接的目标.通过将href属性设定不同类型的值可以使链接指向不同类型的链接地址:内部链接.外部链接.锚链接. 内部链接指的是同 ...
- 性能优化工具---iostat
Iostat (参考 man iostat) 可选项: -c为汇报CPU的使用情况: -d为汇报磁盘的使用情况: -k表示每秒按kilobytes字节显示数据: -t为打印汇报的时间: -v表示打印出 ...
- Android屏幕适配与切图_汇总
首先和最后,还是先看好官方文档:http://developer.android.com/guide/practices/screens_support.html 对应的翻译blog有牛人做了:And ...
- 对常量的引用(reference to const)的一般用途(转载)
如果是对一个常量进行引用,则编译器首先建立一个临时变量,然后将该常量的值置入临时变量中,对该引用的操作就是对该临时变量的操作.对C++常量引用可以用其它任何引用来初始化:但不能改变. 关于引用的初始化 ...
- VC++学习之网络编程中的套接字
VC++学习之网络编程中的套接字 套接字,简单的说就是通信双方的一种约定,用套接字中的相关函数来完成通信过程.应用层通过传输层进行数据通信时,TCP和UDP会遇到同时为多个应用程序进程提供并发服务的问 ...
- C语言与管道
int main() { int s; int n; float avg; scanf("%d,%d",&s,&n); //特别注意的地方 // scanf(&qu ...
- hdu 3572 Escape 网络流
题目链接 给一个n*m的图, 里面有一些点, '.'代表空地, '#'代表墙, 不可以走, '@'代表大门, 可以有多个, 'X'代表人, 问所有人都走出大门需要的最短时间, 每一时刻一个格子只能有一 ...
- 关于BFC
参考 http://www.html-js.com/article/1866(很棒! 还有栗子) http://www.cnblogs.com/lhb25/p/inside-block-format ...
- Java中synchronized注意点
之前一直以为 synchronized 加在方法前面就只有一个线程能访问了,项目中碰到一个问题,一个类的不同对象,同时访问加了 synchronized的方法 同样是可以访问的,那是因为 synchr ...