Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0127 - Word Ladder

LeetCode 0127 - Word Ladder

作者头像
Reck Zhang
发布于 2021-08-11 06:37:07
发布于 2021-08-11 06:37:07
18200
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Word Ladder

Desicription

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 transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
private:
    bool isNeighbor(string& a, string& b) {
        int cnt = 0;
        for(int i = 0; a[i]; i++) {
            if(a[i] != b[i])
                cnt++;
        }
        return cnt == 1;
    }
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        queue<string> cur;
        queue<string> next;
        cur.push(beginWord);
        int step = 2;
        while(cur.size()) {
            string tmp = cur.front();
            cur.pop();
            for(auto& it : wordList) {
                if(it == "" || !isNeighbor(it, tmp))
                    continue;
                if(it == endWord)
                    return step;
                next.push(it);
                it = "";
            }
            if(cur.empty()){
                step++;
                swap(cur, next);
            }
        }    
        return 0;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 0126 - Word Ladder II
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
Reck Zhang
2021/08/11
1800
​LeetCode刷题实战126:单词接龙 II
https://leetcode-cn.com/problems/word-ladder-ii/
程序员小猿
2021/01/19
3330
【leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
阳光岛主
2019/02/19
4160
Leetcode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord to endWord, such that: Only one letter can be changed at a time Each intermediate word must exist in the word list Fo
triplebee
2018/01/12
5470
​LeetCode刷题实战127:单词接龙
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/01/20
5250
​LeetCode刷题实战127:单词接龙
字符串高阶经典题
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:
王脸小
2019/11/06
7980
算法细节系列(20):Word Ladder系列
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/72469345
用户1147447
2019/05/26
9230
LeetCode127. 单词接龙
思想是广度优先遍历 //字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列: // // // 序列中第一个单词是 beginWord 。 // 序列中最后一个单词是 endWord 。 // 每次转换只能改变一个字母。 // 转换过程中的中间单词必须是字典 wordList 中的单词。 // // // 给你两个单词 beginWord 和 endWord 和一个字典 wordList ,找到从 beginWord 到 endWord 的 最
周杰伦本人
2022/10/25
3400
【一天一大 lee】单词接龙 (难度:中等) - Day20201105
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
前端小书童
2020/11/11
4790
【一天一大 lee】单词接龙 (难度:中等) - Day20201105
LeetCode 127. 单词接龙
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
韩旭051
2020/11/24
5210
【leetcode】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
阳光岛主
2019/02/19
4340
☆打卡算法☆LeetCode 127. 单词接龙 算法解析
“给定两个单词beginWord和endWord,以及一个字典wordList,找出并返回所有从beginWord到endWrod之间的最短转换序列中的单词数目。”
恬静的小魔龙
2022/08/07
3680
☆打卡算法☆LeetCode 127. 单词接龙  算法解析
米哈游笔试原题,网友直呼太难!
这道题是力扣的第127题,难度为困难,一网友在米哈游的笔试中遇到这题。实际上这题的代码量还挺多的,如果是在机试上直接敲代码我还能理解,但在笔试中没有任何代码提示的情况下能把它完整的写出来真的是不容易。
数据结构和算法
2024/10/11
1400
米哈游笔试原题,网友直呼太难!
☆打卡算法☆LeetCode 126. 单词接龙 II 算法解析
“给定两个单词beginWord和endWord,以及一个字典wordList,找出并返回所有从beginWord到endWrod之间的最短转换序列。”
恬静的小魔龙
2022/08/07
3360
☆打卡算法☆LeetCode 126. 单词接龙 II 算法解析
【BFS最短路问题】单词接龙
​ 字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk:
利刃大大
2025/03/05
1160
【BFS最短路问题】单词接龙
每日一练-单词接龙、矩阵中的最长递增路径、Z 字形变换
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1k2biw8u5kw2
共饮一杯无
2022/11/28
2720
每日一练-单词接龙、矩阵中的最长递增路径、Z 字形变换
Leetcode No.126 单词接龙 II(BFS)
按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:
week
2022/01/06
2360
Leetcode No.126 单词接龙 II(BFS)
2021-09-07:单词接龙 II。按字典 wordList 完成从单词 begi
2021-09-07:单词接龙 II。按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:每对相邻的单词之间仅有单个字母不同。转换过程中的每个单词 si(1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。sk == endWord,给你两个单词 beginWord 和 endWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWord 到 endWord 的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 beginWord, s1, s2, ..., sk 的形式返回。力扣126。
福大大架构师每日一题
2021/09/07
3940
程序员面试金典 - 面试题 17.22. 单词转换(BFS)
给定字典中的两个词,长度相等。 写一个方法,把一个词转换成另一个词, 但是一次只能改变一个字符。 每一步得到的新词都必须能在字典中找到。
Michael阿明
2020/07/13
6160
Leetcode | 第7节:DFS,BFS
这一节我们介绍一下DFS和BFS,也就是深度优先搜索和广度优先搜索。搜索算法也是很多题目我们所会考虑的第一个算法,因为想法直接而易想。本来是要介绍树有关的内容的,但是研究了一下材料发现,其实树相关的题目还是挺多需要用到我们这一节说到的搜索算法的,所以我们就临时换了一下介绍顺序。
学弱猹
2021/08/10
6540
Leetcode | 第7节:DFS,BFS
相关推荐
LeetCode 0126 - Word Ladder II
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验