[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 ...
随机推荐
- 227. Basic Calculator
1. 问题描述 Implement a basic calculator to evaluate a simple expression string. The expression string c ...
- JavaScript之怎样获取元素节点
JavaScript获取元素节点一共有三种方法,分别是通过元素ID.通过标签名字和通过类名字来获取: 1.通过元素ID属性的ID值来获得元素对象-getElementById() DOM提供了一个名为 ...
- 保护眼睛,绿豆沙颜色的RGB值和HSL值
现在的人尤其是职场中人,每天都得花很长时间对着电脑,对眼睛的伤害很大,其实我们可以对电脑进行一个简单的设置,把窗口背景设置成绿豆沙颜色的,对眼睛的保护很有帮助的. 下面是绿豆沙颜色的RGB值和HSL值 ...
- jquery + ajax调用后台方法
前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...
- Ie浏览器TextBox文本未居中
Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中 vertical-align:middle
- 星际地图制作中OB无建筑 退出问题
星际地图制作中OB玩家没有建筑强制退出问题,目前 用下面的方法解决 ob玩家分到一个组,触发里面 延时几秒 我设置的2秒 KILL掉这个组的建筑就行~
- Linux 网络编程基础(3) -- 数据的IO
首先介绍两个数据结构及相关的操作函数:struct iovec.struct msghdr struct iovec { void * iov_base; /*向量的缓冲地址*/ size_t ...
- Android倒计时Button
最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下: 1.TimeButton 自定义倒计时Button package com.example.timebu ...
- 《Java4Android视频教程》学习笔记(三)
一:抽象类 接口 1.使用abstract修饰抽象类 抽象函数 2.一个类中有一个或者多个抽象函数,必须定义为抽象类 3.抽象类可以不含有抽象函数 4.抽象类不可以生成对象 tip:如果一个代码在语意 ...
- 补丁(patch)的制作与应用
命令简介 用到的两个命令是diff和patch. diff diff可以比较两个东西,并可同时记录下二者的区别.制作补丁时的一般用法和常见选项为: diff [选项] 源文件(夹) 目的文件(夹) - ...