[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 ...
随机推荐
- 执行startx后Ubuntupassword正确进不去的问题
今天在命令行里敲了 startx ,然后系统重新启动.输入password后,跳转到一下界面.之后又返回到登陆界面.一直这样循环输入password.进不去系统. 然后不得不用手机在网上查找解决的方法 ...
- Objective-C基础笔记(2)@property和@synthesize
先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下: Person.h文件 #import <Foundation ...
- 推荐两个不错的CAD二次开发(.Net)手册
推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...
- The Rose
Some say love it is a river 有人说爱是一条河 that drowns the tender reed 会淹没轻柔的芦苇 Some say love it is a razo ...
- List用法
定义一个类: using System;using System.Collections.Generic;using System.Linq;using System.Web; namespace W ...
- [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
笔者将在windows下的eclipse写的代码拷贝到MAC下,发现中文会出现乱码. 最初笔者遇到这个问题的时候,在网络上寻找了解决办法,出来的第一个网页(http://blog.csdn.net/w ...
- leetcode Longest Substring Without Repeating Characters python
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...
- Html页面加回滚
<div class="top-box"> <img src=" class="youlink-img" /><br / ...
- 保存属性至xml并读取
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...