leetcode127单词接龙刷题打卡

127. 单词接龙

字典 wordList 中从单词 beginWordendWord转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk

  • 每一对相邻的单词只差一个字母。
  • 对于 1 <= i <= k 时,每个 si 都在 wordList 中。注意, beginWord 不需要在 wordList 中。
  • sk == endWord

给你两个单词 beginWordendWord 和一个字典 wordList ,返回 beginWordendWord最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0

示例 1:

复制代码
输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
输出:5
解释:一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。

示例 2:

复制代码
输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
输出:0
解释:endWord "cog" 不在字典中,所以无法进行转换。

提示:

  • 1 <= beginWord.length <= 10
  • endWord.length == beginWord.length
  • 1 <= wordList.length <= 5000
  • wordList[i].length == beginWord.length
  • beginWordendWordwordList[i] 由小写英文字母组成
  • beginWord != endWord
  • wordList 中的所有字符串 互不相同

题解思路

在学习图论的时候做的一道题,完全没有思路,之前做的题都是二维矩阵,有个图样,轮到每个点有四个方向供我选择,这道题只有一个单词列表。

待解决问题:

  • 深搜or广搜

  • 如何建图

广搜

这道题应该用广度搜索,题目中要求最短路径,用广搜的话如果遍历到了则就是那么对应的路径就是最短路径

关于建图

之前是有一个图,然后我们遍历到每一个点后尝试该点的四个方向,

这道题没有图我们遍历到一个单词后该如何尝试方向呢?

题目要求每次只改变一个单词的一个字母且改变后的单词需要出现在wordlist中,我们就可以基于改变一个字母来确定遍历的方向

遍历方向为:

到每一个单词时,有word_length*26个方向供我们遍历

c++ 复制代码
while(!que.empty()){
    string word = que.front(); que.pop();
    int path = visited[word]; // unordered_map
    
    for(int i = 0; i < word.size(); i++){
        
        string newWord = word;
        for(int j = 0; j < 26; j++){
            newWord[i] = j + 'a';
            // 入队处理 and 终止条件处理
        }
        
    }
}

我们对比一些二维矩阵的广搜核心框架

c++ 复制代码
while(!que.empty()){
    pair<int, int> cur = que.front(); que.pop();
    int curx = cur.first;
    int cury = cur.second;
    
    for(int i = 0; i < 4; i++){
        
        int nextx = curx + dir[i][0];
        int nexty = cury + dir[i][1];
        // 入队处理 and 终止条件处理
        
    }
}

通过广搜框架我们不需要显示的建图就可以像图一样搜索。

完整代码

c++ 复制代码
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> wordSet(wordList.begin(), wordList.end());
        if(wordSet.find(endWord) == wordSet.end()) return 0;
        unordered_map<string, int> visited;
        visited[beginWord] = 1;
        queue<string> que;
        que.push(beginWord);
        
        while(!que.empty()){
            string word = que.front(); que.pop();
            int path = visited[word];
            for(int i = 0; i < word.size(); i++){
                string newWord = word;
                for(int j = 0; j < 26; j++){
                    newWord[i] = j + 'a';
                    if(newWord == endWord) return path + 1;
                    if(wordSet.find(newWord) != wordSet.end() 
                            && visited.find(newWord)  == visited.end()){
                        visited[newWord] = path + 1;
                        que.push(newWord);
                    }
                }
            }
        }
        return 0;
        
    }
};
相关推荐
free-elcmacom1 分钟前
C++三种参数传递方式:从交换函数看值、指针与引用的区别
开发语言·c++
柏木乃一6 分钟前
Linux线程(8)基于单例模式的线程池
linux·运维·服务器·c++·单例模式·操作系统·线程
Trouvaille ~21 分钟前
【贪心算法】专题(三):排序、博弈与区间的贪婪法则
c++·算法·leetcode·青少年编程·面试·贪心算法·蓝桥杯
IT199531 分钟前
C++工作笔记-动态库中的单例类存储方式
开发语言·c++·笔记
一起搞IT吧1 小时前
Android功耗系列专题理论之十五:相机camera功耗问题分析方法
android·c++·数码相机·智能手机·性能优化
C+++Python1 小时前
C++ 策略模式实战:从原理到落地
开发语言·c++·策略模式
爱装代码的小瓶子1 小时前
【c++与Linux进阶】线程篇 -互斥锁
linux·c++·算法
一叶落4381 小时前
LeetCode 149. 直线上最多的点数(C语言详解 | 斜率 + 最大共线点)
数据结构·c++·算法·leetcode
仰泳的熊猫1 小时前
题目2267:蓝桥杯2016年第七届真题-取球博弈
数据结构·c++·算法·蓝桥杯·深度优先·图论
Genevieve_xiao1 小时前
【差分】差分的理解与基础题型总结
数据结构·c++·算法