题目:

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. IOC容器特性注入第一篇:程序集反射查找

    学习kooboo的框架发现它的注入容器方法比较特别,同样是利用MVC的注入点,但它是查找网站下面bin所有的DLL利用反射查找特性找到对应的服务注入到容器. 这样的好处很简单:完全可以不用关心IOC容 ...

  2. Schema Workbench 开发mdx和模式文件

    一.前言 安装了saiku之后,每次修改schema文件,非常耗时,每次都要经历若干步骤:修改xml.上传.重启才能生效,并且非常不利于学习和理解MDX和模式文件,踌躇之际,发现了这个工具,十分小巧方 ...

  3. AX3空Invoice明细问题

    用户反映出现invoice无法打印明细问题,看截图 考虑再三,去查看invoice ID,居然出现类似ABABAB\的字符,就是最后一个\有问题, 修改VendInvoiceJour,VendInvo ...

  4. MMO之禅(二)职业精神

    MMO之禅(二)职业精神 --心态 目标 信仰 Zephyr 201304 继续上篇,继续讲什么?打了很多腹稿点滴,从引擎架构,到上层数据.逻辑模块规划,想了很多,临起笔,却总发觉四顾心茫然,乱不可言 ...

  5. 基于Maven构建整合SpringMVC+Mybtis+Druid

    前几天趁空闲时间整合了下SpringMVC+Mybatis+Druid,这里小记录下,这个Demo是基于Maven构建的,数据源用的是阿里巴巴温少的开源项目Druid,数据库用的是Mysql. 由于E ...

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

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

  7. 【原】Configuring Oracle Data Guard In Physical Standby Database

    作者:david_zhang@sh [转载时请以超链接形式标明文章] http://www.cnblogs.com/david-zhang-index/p/5042640.html参照文档:https ...

  8. 解决错误: java.lang.NoClassDefFoundError: antlr/RecognitionException

    网络质量不好的情况下,访问maven.org网站下载jar包,很有可能下载的包不完整或损坏的(表面看不出来):所以,最好的办法就是-直接到maven网站下载,然后放到对应的.m2目录,然后eclips ...

  9. jdbc根据实例名 连接 sql server

    jdbc:sqlserver://PC;instanceName=sql2012;databaseName=xxxxx

  10. shell 和awk性能对比

    time for ((i=0;i<10000;i++)) do ((sum+=i)); done real    0m0.086suser    0m0.079ssys    0m0.007s ...