题目描述

给定两个单词(初始单词和目标单词)和一个单词字典,请找出所有的从初始单词到目标单词的最短转换序列的长度:
  1. 每一次转换只能改变一个单词
  2. 每一个中间词都必须存在单词字典当中
例如:
给定的初始单词start="hit",
目标单词end ="cog"。
单词字典dict =["hot","dot","dog","lot","log"]
一个最短的转换序列为"hit" -> "hot" -> "dot" -> "dog" -> "cog",
返回长度5

注意:
如果没有符合条件的转换序列,返回0。
题目中给出的所有单词的长度都是相同的
题目中给出的所有单词都仅包含小写字母

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 length5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.
      class Solution {
      public:
          int ladderLength(string start, string end, unordered_set<string> &dict) {
              queue<string>Q;
              Q.push(start);
              int res=1;
              while (!Q.empty()){
                  int qsize=Q.size();
                  while (qsize--){
                      string cur_front=Q.front();
                      Q.pop();
                      int size=cur_front.size();
                      for (int i=0;i<size;i++)
                      {
                          char ch=cur_front[i];
                          for (int j=0;j<26;j++){
                              cur_front[i]='a'+j;
                              if (cur_front==end) return res+1;
                              if (dict.find(cur_front)!=dict.end())
                              {
                                  Q.push(cur_front);
                                  dict.erase(cur_front);
                              }
                          }
                          cur_front[i]=ch;
                      }
                  }
                  res++;
              }
              return 0;
          }
      };

leetcode25word-ladder的更多相关文章

  1. [LeetCode] Word Ladder 词语阶梯

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

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

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

  3. LeetCode:Word Ladder I II

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

  4. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  5. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  6. 18. Word Ladder && Word Ladder II

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

  7. [Leetcode][JAVA] Word Ladder II

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

  8. LeetCode127:Word Ladder II

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

  9. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

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

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

随机推荐

  1. MySQL 向表中插入、删除数据

    一.向表中插入一条信息 1.查看表中的数据 mysql> SELECT * FROM user; +----+---------+----------+ | id | account | pas ...

  2. Python中list的合并

    ①差集 方法一: if __name__ == '__main__':     a_list = [{'a' : 1}, {'b' : 2}, {'c' : 3}, {'d' : 4}, {'e' : ...

  3. 使用AirtestProject+pytest做支付宝小程序UI自动化测试

    一,前言 1,背景 因公司业务需要做支付宝小程序的UI自动化测试,于是在网上查找小程序的自动化资料,发现微信小程序是有自己的测试框架的,但几乎找不到支付宝小程序UI自动化测试相关的资料.白piao失败 ...

  4. mysql DISTINCT选取多个字段,获取distinct后的行信息

    背景:      a表保存关联关系,通过ACode 获取该关系中的所有 BCode, 并获取所有Bcode-max(Bvrsn)的信息  Bnm 表a 表b          循序渐进:        ...

  5. 网易新闻精彩评论集合(慢慢收集ing)

    一.刚才在停车场看一男的开个Q7,怎么也停不进去.我迅速把车停好要过去帮忙,他死活不同意.我说,你刚也看见了我的停车技术了,肯定不能给你刮了.他干脆把窗户摇上了.如今的社会啊,人与人的互信程度为什么就 ...

  6. 多测师讲解自动化测试 _RF定位iframe框_高级讲师肖sir

    Open Browser https://mail.163.com/ gc Maximize Browser Window sleep 2 #进入if框 Comment Input Text name ...

  7. [论文阅读笔记] GEMSEC,Graph Embedding with Self Clustering

    [论文阅读笔记] GEMSEC: Graph Embedding with Self Clustering 本文结构 解决问题 主要贡献 算法原理 参考文献 (1) 解决问题 已经有一些工作在使用学习 ...

  8. 怎样学好 java ?

    浅谈Java的学习之路--怎样学好JAVA ?Java - 近10年来计算机软件发展过程中的传奇,其在众多开发者心中的地位就如"屠龙刀"."倚天剑". Java ...

  9. go语言安装使用

    go语言安装使用 下载地址 https://golang.google.cn/dl/ https://studygolang.com/dl windows https://studygolang.co ...

  10. lumen-ioc容器测试 (4)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...