[LeetCode]题解(python):127-Word Ladder
题目来源:
https://leetcode.com/problems/word-ladder/
题意分析:
和上一题目类似,给定一个beginWord和一个endWord,以及一个字典list。这题需要的是要beginWord转化成endWord的最短路径长度。
题目思路:
最短路的思路。将beginWord放进队列,如果队列不为空,那么取出第一个数,将其周围的在字典的字符放进队列,直到周围的存在endword。
代码(python):
import Queue
class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: Set[str]
:rtype: int
"""
q,ans = Queue.Queue(),{}
q.put(beginWord);wordList.add(endWord)
ans[beginWord] = 1
while not q.empty():
tmp = q.get()
if tmp == endWord:
return ans[endWord]
for i in range(len(tmp)):
part1,part2 = tmp[:i],tmp[i+1:]
for j in 'abcdefghijklmnopqrstuvwxyz':
if tmp[i] != j:
newword = part1 + j + part2
if newword in wordList:
q.put(newword)
ans[newword] = ans[tmp] + 1
wordList.remove(newword)
return 0
[LeetCode]题解(python):127-Word Ladder的更多相关文章
- leetcode 127. Word Ladder、126. Word Ladder II
		
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
 - 127. Word Ladder(M)
		
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
 - [LeetCode] 127. Word Ladder 单词阶梯
		
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
 - Leetcode#127 Word Ladder
		
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
 - 【LeetCode】127. Word Ladder
		
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
 - LeetCode 127. Word Ladder 单词接龙(C++/Java)
		
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
 - 【LeetCode】127. Word Ladder 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
 - leetcode@ [127] Word Ladder (BFS / Graph)
		
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
 - leetcode 127. Word Ladder  ----- java
		
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
 - [leetcode]127. Word Ladder单词接龙
		
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
 
随机推荐
- Gridland(规律)
			
Gridland Time Limit: 2 Seconds Memory Limit: 65536 KB BackgroundFor years, computer scientists ...
 - C++中的随机数函数(
			
标签:ul 随机数 c 整数 max 教育 C++中产生随机数种子对于刚開始学习的人一直都非常困惑.大家知道,在C中有专门的srand(N)函数能够轻松实现这一功能,然而在C++中则要复杂一些.以下 ...
 - C++重载运算符的规则
			
(1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载. 例如,有人觉得BASIC中用“* *”作为幂运算符很方便,也想在C++中将“* *”定义为幂运算符,用“3* *5”表示3 ...
 - Android解析XML
			
在Android平台上可以使用Simple API for XML(SAX) . Document Object Model(DOM)和Android附带的pull解析器解析XML文件. 下面是本例子 ...
 - Drupal 7 建站学习手记(四):怎样改动Nivo Slider模块的宽高
			
背景 Nivo Slider模块默认大小是用的height: 100%, width 100%, 但IE7及下面的浏览器是不支持百分比宽高的, 而我的项目目标用户基本都是使用XP系统,项目需求是必须兼 ...
 - 存储过程中调用EXECUTE IMMEDIATE的“权限不足”问题
			
使用plsql 动态创建表时,用户需要具有create any table 权限 例如: create or replace procedure create_table_test is tmpstr ...
 - eclipse使用技巧---使用正则表达式查找替换
			
1,Eclipse ctrl+f 打开查找框2,选中 Regular expressions (正则表达式) 去掉/* */(eclipse) /\*(.|[\r\n])*?\*/去掉/ ...
 - ##DAY9 UITabBarController
			
##DAY9 UITabBarController UIViewController的tabBarController UIViewController的tabBarItem #pragma mark ...
 - 小鱼提问1  类中嵌套public修饰的枚举,外部访问的时候却只能Class.Enum这样访问,这是为何?
			
/// <summary> /// 常量等定义 /// </summary> public class General { /// <summary> /// 文件 ...
 - 用C++写一个简单的订阅者
			
打开一个终端,进入到beginner_tutorials包下面: cd ~/catkin_ws/src/beginner_tutorials 建立文件src/listener.cpp: vim src ...