[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 ...
随机推荐
- 随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)
感觉微软在面向对象三大原则中,封装性运用的最为突出,它会将一些复杂的算法,结构,功能代码进行封装,让程序员在使用时十分得心应手,如关键字里的foreach和labmda表达式里的Foreach等等,今 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- SQL Server索引进阶第十一篇:索引碎片分析与解决
相关有关索引碎片的问题,大家应该是听过不少,也许也很多的朋友已经做了与之相关的工作.那我们今天就来看看这个问题. 为了更好的说明这个问题,我们首先来普及一些背景知识. 知识普及 我们都知道,数据库中的 ...
- iWeb峰会见闻
8.16去参加了iWeb峰会,一大早8点过10分就到了,发现外面已经排起了长队(说明影响力越来越大,关注的人越来越多了.) 此次大会参与的企业也越来越多,当然是有目的而来~ 上午 google商业合作 ...
- 内容高度小于窗口高度时版权div固定在底部
<!doctype html><html><head><meta charset="utf-8"><title>文档内容 ...
- .Net 数组去除重复项
string str = "1/1/12/13/15/16/15//"; ] { '/' }, StringSplitOptions.RemoveEmptyEntries); Li ...
- 多个tab切换传参
var _ =peopleslive.h5; _.main=function(){ //集赞排名1 $('#tab1 .tab_nav').find('li').click (function() { ...
- poj 3348 Cows 求凸包面积
题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include ...
- C/C++指针知识整理(一)
1.指针(变量)的类型 把指针声明语句里的指针名字去掉,剩下的部分就是这个指针的类型.这是指针本身所具有的类型. (1)int*ptr; //指针的类型是 int* (2) char*ptr;//指针 ...
- SMT贴片红胶基本知识
SMT贴片红胶是一种聚稀化合物,与锡膏不同的是其受热后便固化,其凝固点温度为150℃,这时,红胶开始由膏状体直接变成固体. SMT贴片机装贴贴片具有粘度流动性,温度特性,润湿特性等.根据红胶的这个特性 ...