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. Educational Codeforces Round 15 A dp

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. 将Excel生成实体类

    package com.excel.test; import java.util.List; public class createUtil { public static String append ...

  3. c++将引用作为函数的参数---6

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量别名.这种传递参数 的方法称为按引用传递. ...

  4. Apache的虚拟主机配置

    使用虚拟主机要先取消中心主机,注释掉DocumentRoot #DocumentRoot "/www/htdoc" 虚拟主机的单独配置: 用户认证 访问日志 错误日志 别名 脚本别 ...

  5. spark sql 访问hive数据时找不mysql的解决方法

    我尝试着在classpath中加n入mysql的驱动仍不行 解决方法:在启动的时候加入参数--driver-class中加入mysql 驱动 [hadoop@master spark-1.0.1-bi ...

  6. PC端的混合应用通讯问题

    exe使用C#开发,内嵌HTML页面HTML页面与exe程序的通讯方式可以使用以下方式: HTML通知exe:C#有个titlechange事件,可以监听内部HTML的title,那么HTML就可以通 ...

  7. 黑马程序员——JAVA基础之set集合

    ------- android培训.java培训.期待与您交流! ---------- Set:       元素是无序(存入和取出的顺序不一定一致),元素不可以重复.    Set接口中常用的类: ...

  8. ADC 分辨率和精度的区别

    分辨率和精度这两个,经常拿在一起说,才接触的时候经常混为一谈.对于ADC来说,这两样也是非常重要的参数,往往也决定了芯片价格,显然,我们都清楚同一个系列,16位AD一般比12位AD价格贵,但是同样是1 ...

  9. Linux-某电商网站流量劫持案例分析与思考

    [前言] 自腾讯与京东建立了战略合作关系之后,笔者网上购物就首选京东了.某天在家里访问京东首页的时候突然吃惊地发现浏览器突然跳到了第三方网站再回到京东,心里第一个反应就是中木马了. 竟然有这样的事,一 ...

  10. 【Unity3D基础教程】给初学者看的Unity教程(七):在Unity中构建健壮的单例模式(Singleton)

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! 该博客中的代码均出自我的开源项目 : 迷你微信 ...