Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

====================

广度优先搜索bfs,

题目需要注意的地方:

  1. 如果没有可以转变的单词阶梯,需要返回-1,
  2. 所有的单词由相同的长度
  3. 所有的单词都是小写字母
  4. 转换过程中,一次只能转变一个字母
  5. 输入有开始单词,有结束单词,还有一个词典集合set,
  6. 转变过程中的所有 中间单词表示  都是从词典集合set中存在的
  7. 输出的是 单词阶梯的长度(包括开始单词和结束单词)

经典bfs搜索

  • 定义一个unorder_map<string,int> m;  保存的是在转变过程中的  中间单词出现的位置,即"hit" -> "hot" -> "dot" -> "dog" -> "cog",m[hit]=1,m[hot]=2
  • 定义一个queue<string> q;保存的是在广度优先搜索bfs的搜索次序,因为bfs一定会用到队列,就像深度优先搜索dfs一定会用到栈一样。这个队列q会把搜索路径中所有经过的  单词中间节点  都过滤一边。
  • 因为我们只是求单一的解,找到即结束。所以不用保存搜索路径,不用找到所有的结果。
  • 看代码。
int ladderLength(string beginWord, string endWord,
unordered_set<string>& wordList) {
unordered_map<string,int> m;
queue<string> q;
m[beginWord] = ;
q.push(beginWord);
while(!q.empty()){
string word = q.front();
q.pop();
for(int i = ;i<(int)word.size();i++){
string newWord = word;
for(char ch = 'a';ch<='z';++ch){///因为我们每次只能改变一个字母,所以这里是一重循环遍历'a'->'z',看是否能在字典集合中找改变后的 单词值
newWord[i] = ch;///这就是改变一个字母后的单词值
if(newWord==endWord) return m[word]+;///此时已经能够找到一条路径了,直接返回就行了。
if(wordList.find(newWord)!=wordList.end() && ///在字典集合中存在,
m.find(newWord)==m.end()){///改变一个字母后的单词在 哈希表m中不存在,是为了防止出现hit->hit的情形。
//cout<<"q.size()="<<q.size()<<endl;
//cout<<"word->"<<word<<endl;
//cout<<"newWord->"<<newWord<<endl<<endl;
q.push(newWord);///放入q中,为bfs做准备
m[newWord] = m[word]+;///新单词在 单词阶梯中的位置
}
}///for
}///for
}
return ;
}

127 Word Ladder的更多相关文章

  1. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  4. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

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

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

  6. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  7. leetcode 127. Word Ladder ----- java

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

  8. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  9. [leetcode]127. Word Ladder单词接龙

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

随机推荐

  1. SAP 默认的连接端口

    3708.3908.4008.32<instance number> 如instance number是00的话,就是3200,这是给 disp+working process 用的. 3 ...

  2. db2 字符串转换 数字

    今天使用聚合函数的时候 发现 varchar类型的是不可用的,所以呢就开始想办法解决 用到了转换函数cast(s.score as bigint) 然后有一个问题就是如果数据为空的话就会发生转换错误. ...

  3. 启动tomcat不出现命令窗口

    有个软件要安装在U盘中,B/S结构,用tomcat做应用服务器,客户要求tomcat不能注册为系统服务,启动时tomcat启动时不能出现命令行窗口,怎么实现? 根据你的问题描述,猜测你的部署系统是Wi ...

  4. Open经验库网址

    http://www.open-open.com/lib/view/open1436094840774.html

  5. php部分--面向对象三大特性-封装(另加连续调用的一个例子)、继承(重写、重载的例子)、多态;

    一.封装性: 目的:为了使类更加安全. 做法:1设置私有成员 2在类中建方法,访问私有成员 3在方法里边加控制(if) 私有成员访问的两种方法: 方法一:set(可写) get(可读)做方法(可读可写 ...

  6. linux网络故障解决方法

    一.检测工具 tcpdump:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的“头”完全截获 ...

  7. Android——GridView

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  8. Java——异常

     /* * 异常:   是在运行时期  发生的 不正常情况. *  在java中类的形式对不正常情况进行了描述和封装对象. * * 描述不正常的情况类,就成为异常. * * 问题很多,就意味着描述 ...

  9. Android-Universal-Image-Loader开源项目的简要说明及使用实例

    本文转载于:http://www.cnblogs.com/hsx514/p/3460179.html 一.核心类的说明及相关参数的说明 ImageLoaderConfiguration 1.作用:为I ...

  10. ubuntu下如何安装wxpython

    1.运行时缺失wx库,如何安装 Error:ImportError: No module named wx 解决方法:sudo apt-get install python-wxgtk2.8 pyth ...