一. 题目描写叙述

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

• Only one letter can be changed at a time

• Each intermediate word must exist in the dictionary

For example, Given:

start = “hit”

end = “cog”

dict = [“hot”,”dot”,”dog”,”lot”,”log”]

As one shortest transformation is ”hit” -> ”hot” -> ”dot” -> ”dog” -> ”cog”, return its length 5.

Note:

• Return 0 if there is no such transformation sequence.

• All words have the same length.

• All words contain only lowercase alphabetic characters.

二. 题目分析

參考链接:http://www.mamicode.com/info-detail-448603.html

能够将这道题看成是一个图的问题。我们将题目映射到图中,顶点是每个字符串,然后两个字符串假设相差一个字符则进行连边。

我们的字符集仅仅有小写字母。并且字符串的长度固定,假设是L。那么能够注意到每个字符能够相应的边有25个(26个小写字母去掉自己)。那么一个字符串可能存在的边是25*L条。接下来就是检查这些相应的字符串是否在字典内。就能够得到一个完整的图的结构。

依据题目要求,等价于求这个图中一个顶点到还有一个顶点的最短路径。我们一般用BFS广度优先。

这道题。我们仅仅能用最简单的办法去做,每次改变单词的一个字母。然后逐渐搜索。这种求最短路径,树最小深度问题用BFS最合适。

和当前单词相邻的单词,就是和顶点共边的还有一个顶点。是对当前单词改变一个字母且在字典内存在的单词。

找到一个单词的相邻单词,增加BFS队列后。我们要从字典内删除。由于不删除会造成相似hog->hot->hog这种死循环。并且删除对求最短路径没有影响,由于我们第一次找到的单词肯定是最短路径。我们是层序遍历去搜索的,最早找到的一定是最短路径。即使后面的其它单词也能转换成它。路径肯定不会比当前的路径短。

这道题仅要求求出最短路径长度,不须要求输出最短路径,所以能够删除这个单词。

BFS队列之间用空串”“来标示层与层的间隔,每次碰到层的结尾,遍历深度+1。进入下一层。

三. 演示样例代码

class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(start.size() == 0 || end.size() == 0) return 0; queue<string> wordQ;
wordQ.push(start);
wordQ.push("");
int path = 1; while(!wordQ.empty())
{
string str = wordQ.front();
wordQ.pop(); if(str != "")
{
int len = str.size();
for(int i = 0; i < len; i++)
{
char tmp = str[i]; for(char c = 'a'; c <= 'z'; c++)
{
if(c == tmp) continue;
str[i] = c;
if(str == end) return path + 1; //假设改变后的单词等于end 返回path+1
if(dict.find(str) != dict.end())
{
wordQ.push(str);
dict.erase(str); //字典内删除这个词 防止重复走
}
}
str[i] = tmp; //重置回原来的单词
}
}
else if(!wordQ.empty())
{
//到达当前层的结尾。并且不是最后一层的结尾
path++;
wordQ.push("");
}
}
return 0;
}
};

leetcode笔记:Word Ladder的更多相关文章

  1. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  3. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  5. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  6. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

  7. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  9. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  10. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. [bzoj3306]树——树上倍增+dfs序+线段树

    Brief Description 您需要写一种数据结构,支持: 更改一个点的点权 求一个子树的最小点权 换根 Algorithm Design 我们先忽略第三个要求. 看到要求子树的最小点权,我们想 ...

  2. hdu 1175 连连看 (深搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...

  3. python 学习笔记 多进程

    要让python程序实现多进程,我们先了解操作系统的相关知识 Unix/Linux操作系统提供了一个fork()系统调用,他非常特殊,普通的函数调用,调用一次,返回一次,但是fork调用一次, 返回两 ...

  4. struts标签include传参的问题

    传一个常量过去居然为null, <s:include value="/biz/customer/corp/module/franchisemanageright/corpFranchi ...

  5. javascript字符串中包含特殊字符问题

    我们都知道,在javascript中,字符串写在单引号或者双引号之中.因为这种要求,我们有些时候一些需要的字符串不能够被javascript解析,如下: "We are "Huma ...

  6. java的maven项目(一)

    Maven项目管理工具 Svn + eclipse  约等于  maven量级 1       Maven的简介 1.1     什么是maven 是apache下的一个开源项目,是纯java开发,并 ...

  7. mybatis-config.xml的解释(zz)

    <!-- xml标准格式 --><?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE ...

  8. MATLAB求函数零点与极值

    1.      roots函数 针对多项式求零点(详见MATLAB多项式及多项式拟合) 2.      fzero函数 返回一元函数在某个区间内的的零点. x0 = fzero(@(x)x.^2-3* ...

  9. java 8中撤销永久代,引入元空间

    撤销永久代,引入元空间: 在 JDK 1.8 中,HotSpot 已经没有 “PermGen space”这个空间了,取而代之是一个叫做 Metaspace(元空间) 的东西. Java7中已经将字符 ...

  10. ZCMU新人训练赛-B

     Tom's Meadow Tom has a meadow in his garden. He divides it into N * M squares. Initially all the sq ...