每日OJ题_BFS解决最短路③_力扣127. 单词接龙

ops/2024/10/22 16:30:34/

目录

③力扣127. 单词接龙

解析代码


③力扣127. 单词接龙

127. 单词接龙

难度 困难

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

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

给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 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
  • beginWordendWord 和 wordList[i] 由小写英文字母组成
  • beginWord != endWord
  • wordList 中的所有字符串 互不相同
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {}
};

解析代码

        和力扣433. 最小基因变化一样,如果将每次字符串的变换抽象成图中的两个顶点和一条边的话,问题就变成了边权为 1 的最短路问题。 因此,从起始的字符串开始,来一次 bfs 即可。

class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordListHash(wordList.begin(), wordList.end());unordered_set<string> vis;if(!wordListHash.count(endWord))return 0;int ret = 1;queue<string> q;q.push(beginWord);vis.insert(beginWord);while(!q.empty()){++ret;int size = q.size();while(size--){string t = q.front();q.pop();for(int i = 0; i < t.size(); ++i){for(char ch = 'a'; ch <= 'z'; ++ch){string tmp = t;tmp[i] = ch;if(wordListHash.count(tmp) && !vis.count(tmp)){if(tmp == endWord)return ret;q.push(tmp);vis.insert(tmp);}}}}}return 0;}
};


http://www.ppmy.cn/ops/4929.html

相关文章

PHP命令执行漏洞CVE-2024-1874复现

CVE-2024-1874 PHP命令执行漏洞 影响版本 Affected versions < 8.1.28 < 8.2.18 < 8.3.5 Patched versions 8.1.28 8.2.18 8.3.6 POC 创建一个文件test.php <?php $descriptorspec [STDIN, STDOUT, STDOUT]; $proc proc_open(["test.bat", "\&…

理解文件系统

1.磁盘与物理内存间的数据为什么以4KB交互&#xff1f; 物理内存我们可以看成是多个4KB的单元&#xff0c;每一个4KB也称为页框&#xff1b; 因为文件在文件系统里存储的数据块是一个个4KB的单元&#xff0c;在磁盘上的文件也是被划分成多个4KB单元&#xff0c;每一个4KB也称…

VUE的import store from ‘./vuex/store改为‘ import store from ‘./vuex/store.js‘

ERROR Failed to compile with 1 error 下午5:25:40 error in (webpack)-dev-server/client?http://10.18.173.180:8081/sockjs-node Syntax Error: no such file or directory, open D:\4myroom\H…

微服务架构与Dubbo

一、微服务架构 微服务架构是一种架构概念&#xff0c;旨在通过将功能分解到各个离散的服务中以实现对解决方案的解耦。 分布式系统式若干独立系统的集合&#xff0c;但是用户使用起来好像是在使用一套系统。 和微服务对应的是单体式开发&#xff0c;即所有的功能打包在一个WAR…

机器学习和深度学习--李宏毅(笔记与个人理解)Day17

Day 17Convolutional Neyral Network (CNN) 卷积神经网络一般都用在image 上面比较多一些&#xff0c;所以课程的例子大多数也都是image Image Classification the same size how about for pc? 这里对于tensor 张量这个概念&#xff0c;我还是比较奇怪&#xff0c;在我认为一…

Stable Diffusion 本地部署教程

截至我的最后更新&#xff08;2023年&#xff09;&#xff0c;Stable Diffusion 是一个流行的开源深度学习模型&#xff0c;用于生成高质量的图像。由于它的强大功能和开放访问性&#xff0c;很多开发者和爱好者希望能够在本地环境中部署和使用它。以下是一个基本的本地部署教程…

【树莓派学习】hello,world!

系统安装及环境配置详见【树莓派学习】系统烧录及VNC连接、文件传输-CSDN博客 树莓派内置python3&#xff0c;可以直接利用python输出。

sky12笔记

ROM (read only memory)的初始值用系统函数 r e a d m e m b / readmemb/ readmemb/readmemh把文件读进来 parameter READ_PATH "../rtl/lut.mif"; initial begin$readmemb(READ_PATH ,mem); endalways组合逻辑常见错误 1.敏感列表变量不全&#xff0c;会导致RTL s…