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. yii2 表单提交一直报错 或者页面脚本写ajax,用firbug调试总是找不到地址页面404

    在Yii框架中,为了防止csrf攻击,封装了CSRF令牌验证,使用Yii表单生成页面的时候,如果表单的提交方式为POST,是都会在页面中添加一个隐藏字段: <div style="di ...

  2. 用js实现a链接跳转

    给listxqbottom div添加a跳转链接 <div class="listxqbottom" onclick="location.href='www.bai ...

  3. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  4. js/jsp清空表单2种方法

    js方式清空表单数据的两种方式 方法1:遍历页面元素 /* 清空FORM表单内容  id:表单ID*/  function ClearForm(id) {     var objId = docume ...

  5. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  6. 继承:《原型和原型链(prototype 属性使您有能力向对象添加属性和方法。)》

    二. 原型对象   在JavaScript 中,每当定义一个对象(函数)时候,对象中都会包含一些预定义的属性.其中函数对象的一个属性就是原型对象 prototype.注:普通对象没有prototype ...

  7. Latex 编译错误: ! pdfTeX error (ext4): \pdfendlink ended up in different nesting level than \pd fstartlink. 解决方法

    最近写 AAAI 的文章,下载了其模板,但是蛋疼的是,总是提示错误,加上参考文献总是出错: 如下: ! pdfTeX error (ext4): \pdfendlink ended up in dif ...

  8. MAVEN build ,GOAL plugin ,execution

    http://www.avajava.com/tutorials/lessons/what-are-the-phases-of-the-maven-clean-lifecycle.html https ...

  9. div下的img标签中图片的大小设定

    首先html为: <div class="box1"><img src="images/more-1.jpg"></div> ...

  10. vb.net 动态调用api

    Imports System Imports System.Runtime.InteropServices Public Class DllInvoke Public Sub New(ByVal DL ...