题目:

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

  1. Only one letter can be changed at a time
  2. 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.
  • 解题思路:
  • 这题一开始没啥思路,谷歌一下,才知要用到BFS,既然要用到BFS,那当然形成一个抽象图。这里我们将每一个字符串当做图中一节点,如果两字符串只需通过变化一个字符即可相等,我们认为这两字符串相连。
  • 遍历图中节点时,我们通常会利用一个visit还标识是否访问过,这里我们将处理过的节点直接从dict中删除,以免重复处理。
  • 实现代码:
  • #include <iostream>
    #include <string>
    #include <queue>
    #include <unordered_set>
    using namespace std; /*
    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.
    */
    class Solution {
    public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
    if(start.empty() || end.empty() || dict.empty())
    return 0;
    queue<string> squ[2];//这里需要用到两个队列,因为是bfs,按层遍历,所以需要一层一层进行处理
    squ[0].push(start);
    bool qid = false;
    int minLen = 1;
    while(!squ[qid].empty())
    {
    while(!squ[qid].empty())//处理同一层节点
    {
    string curstr = squ[qid].front();
    squ[qid].pop();
    for(int i = 0; i < curstr.size(); i++)
    { for(char j = 'a'; j <= 'z'; j++)
    {
    if(j == curstr[i])
    continue;
    char t = curstr[i];
    curstr[i] = j;
    if(curstr == end)
    {
    return minLen+1;
    } if(dict.count(curstr) > 0)
    {
    squ[!qid].push(curstr);
    dict.erase(curstr);
    }
    curstr[i] = t;
    } } }
    qid = !qid;//表示将要处理的下一层
    minLen++; }
    return 0; }
    }; int main(void)
    {
    string start("hit");
    string end("cog");
    unordered_set<string> dict;
    dict.insert("hot");
    dict.insert("dot");
    dict.insert("dog");
    dict.insert("lot");
    dict.insert("log");
    Solution solution;
    int min = solution.ladderLength(start, end, dict);
    cout<<min<<endl;
    return 0;
    }

LeetCode126:Word Ladder的更多相关文章

  1. LeetCode127:Word Ladder II

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

  2. leetcode笔记:Word Ladder

    一. 题目描写叙述 Given two words (start and end), and a dictionary, find the length of shortest transformat ...

  3. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

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

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

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

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

  6. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  7. 18. Word Ladder && Word Ladder II

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

  8. [Leetcode][JAVA] Word Ladder II

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

  9. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

随机推荐

  1. JavaScriptSerializer 中的匿名类型 转json

    二:JavaScriptSerializer 中的匿名类型 这个类型我想大家都清楚,不过性能更高的方式应该是用JsonConvert吧,但这个不是本篇讨论的话题,我们重点来看看匿名类型的Json序列化 ...

  2. openssl 学习之从证书中提取RSA公钥N 和 E

    原文链接: http://blog.csdn.net/kkxgx/article/details/19850509 通常数字证书包含很多信息,其中N和E值即我们称为的公钥.如何从PEM 或者DER格式 ...

  3. popupwindow 与 输入法

    有时候popupwindow会被输入法覆盖, 有时候popupwindow会被输入法给顶上去. 而且这个问题还跟theme的windowFullscreen属性相关. 不过这些可以都不用管, 根据项目 ...

  4. 一次受限于操作系统进程数的OOM

    在64bit机上跑应用,结果进程刚起来就挂,就刚起来就挂..还OOM,还fork不出新进程,尼玛,这什么情况? 1. 如果是应用层面OOM,那么不应该任何命令都不被执行了,不应该OS直接crash掉. ...

  5. 记录一个Word操作技巧,很偏门的,鉴于Google很不方便用了,百度起来比较费劲所以记录一下

    拿到一篇文章需要修改时需要将文中某一段带有特定文字的段落删除,比如一段带有“淘宝网”文字的广告性宣传,且这种段落并不是全都一样,数量也很多,不太可能手动一段一段找到Delete,这就可以用这个替换查找 ...

  6. location.hash属性介绍

    location.hash属性介绍 例如URL: http://wwww.a.com/index#rhythmk 通过location.hash 我们将获取到 #rhythmk. 默认浏览器会滚动至i ...

  7. VPN各种常见状态码及修复方法

    1.633错误 :由于Windows系统本身的问题,在PPTP协议连接多次并断开之后,后导致一直出现633错误.参见微软的官方解决方案:http://support.microsoft.com/kb/ ...

  8. 读书笔记_Effective_C++_条款四十四:将与参数无关的代码抽离template

    标题上说“将与参数无关的代码抽离template”,这里的参数既可以指类型,也可以是非类型,我们先来看看非类型的情况. 假定我们要为矩阵写一个类,这个矩阵的行列元素个数相等,是一个方阵,因而我们可以对 ...

  9. Fire!(BFS)

    Fire! Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descr ...

  10. Ubuntu 14.04 载入 JWS 或 访问 jsp异常的解决方法

    前段时间在Ubuntu 14.04中使用 Chrome登录 Webex准备面试的时候发现无法进入在线面试.搞笑的是前一天尝试进入 Webex的时候还一切正常,不过当时Webex的在线面试没有开始.等到 ...