[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 ...
随机推荐
- JDBC增强
JDBC增强 批处理:批量处理sql语句,比如批量添加用户信息. addBatch() //pstmt.addBatch() 就是替换一条一条执行的execute****** executeBat ...
- 从零开始学习UNITY3D(GUI篇)
邻近年底,心也有些散乱,加上工作忙了一阵,在达内培训的课程也落下了不少.对unity3d的热度似乎也有点点下降.痛定思痛,又在淘宝上买了写蛮牛网的视频.总之不管是用任何手段都要逼着自己不要浪费了培训的 ...
- JS把字符串转换为数字的方法
方法: (1)Number(),强制类型转换,接受一个参数. (2)parseInt(),把字符串转换为整形数字,可以接受一个或两个参数,其中第二个参数代表转换的基数,能够正确的将二进制.八进制.十 ...
- 有关android源码编译的几个问题
项目用到编译环境,与源码有些差异不能照搬,关键是连源码都没编译过,下面基本上是行网上照的各种自学成才的分享,病急乱投医了,都记在下面作为参照吧. 1.验证是否编译正确,在终端执行 emulator & ...
- ToDoList-学习中看到的知识盲点
1. java中的volatile关键字的作用 2. java类加载器 3. Android源码编译 4. MediaPlayer的用法 5. Html5和web app
- spring aop简单日志实例
转载自:http://www.blogjava.net/laoding/articles/242611.html 一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近 ...
- PL/SQL编程要点和注意点
http://www.itpub.net/thread-1560427-3-1.html 1. 非关键字小写,关键字大写,用下划线分隔,用前缀区分变量与表列名.不强求变量初始值.2. 永远只捕获可预测 ...
- oracle习题SQL语句练习
表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Varchar2(3) 否 学号(主码) Sname Varchar2(8) 否 学生姓名 Ssex Varchar2( ...
- jjjjQuery选择器
此文为作者自用复习文章 jQuery选择器: 它不仅继承了CSS选择器简洁的语法, 还继承了其获取页面便捷高效的特点, 它还拥有更加完善的处理机制: 但jQuery选择器获取元素后,为该元素添加的是行 ...
- Windows下提升进程权限(转)
from: http://www.oschina.net/code/snippet_222150_19533 windows的每个用户登录系统后,系统会产生一个访问令牌(access token) , ...