题目:

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. FindBugs工具常见问题

    1,AM: Creates an empty jar file entry (AM_CREATES_EMPTY_JAR_FILE_ENTRY)/AM: Creates an empty zip fil ...

  2. iOS开发——高级技术精选&底层开发之越狱开发第一篇

    底层开发之越狱开发第一篇 做越狱开发也有一些时间了,有很多东西想总结一下,希望给他人一些借鉴,也是自己对过去开发经历的一些总结.个人不推荐使用盗版,这里主要以技术介绍为主. 这个系列里面主要介绍怎样进 ...

  3. cordova混合开发:Android中native调用javascript

    今天学习怎么在java中调用javascript方法,做个记录: 第一种方式,这个最简单: loadUrl("javascript:func1()"); 要注意要在devicere ...

  4. css中的position:relative和absolute 属性

    语法: position : static | absolute | fixed | relative 取值: static :默认值.无特殊定位,对象遵循HTML定位规则 absolute :将对象 ...

  5. Visual Studio 2015 开发大量 JavaScript 代码项目程序崩溃的解决方案

    最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...

  6. 修改dll版本号处理未能加载“******”,或找不到动态链接库依赖的项

    <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken=& ...

  7. ubuntu系统从中文环境改成英文环境

      我们在 安装ubuntu server版的时候,有人可能选择了中文环境安装,因为那样好设置时区等参数,可是安装好了后,运行某些命令的时候会有中文乱码提示,看起很是头蛋疼, 我们就需要将其改成英文环 ...

  8. JAVA笔记 之 Thread线程

    线程是一个程序的多个执行路径,执行调度的单位,依托于进程存在. 线程不仅可以共享进程的内存,而且还拥有一个属于自己的内存空间,这段内存空间也叫做线程栈,是在建立线程时由系统分配的,主要用来保存线程内部 ...

  9. tcp/ip协议listen函数中backlog参数的含义与php-fpm的502 Bad Gateway

    To understand the backlog argument, we must realize that for a given listening socket, the kernel ma ...

  10. ubuntu 12.04亮度无法调节和无法保存屏幕亮度解决办法(echo_brightness)

    经过多次更改失败重装后终于在官网的answers找到了解决办法:原文链接 http://askubuntu.com/questions/3841/desktop-doesnt-remember-bri ...