当前位置: 首页 > news >正文

【算法与数据结构】127、LeetCode单词接龙

文章目录

  • 一、题目
  • 二、解法
  • 三、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述

二、解法

  思路分析:示例1为例,hit到达cog的路线不止一条,如何找到最短是关键。广度优先搜索是一圈一圈的搜索过程,一旦找到了结果,一定是最短的。本题也只需要最短转换序列的数目而不需要具体的序列,因此不用去关心下图中线是如何连在一起的。因此最终选择广搜,只要差一个字符说明序列之间是连接的。

  • 本题还是一个无向图,需要用到标记位,标记节点是否走过,否则会陷入死循环。为此我们引入一个unordered_map<string, int> visitMap类型的地图,记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度。
  • 集合是数组类型的,提前转成集合set类型,查找更快。

  根据单词的长度,每次替换其中一个单词,需要用到两个循环,一个循环选择单词中替换的字符位置,另一个用来选择26个字母中的其中一个。然后在单词集合中查找是否存在替换之后的新单词,如果找到将其加入visitMap中。如果新单词是endWord,那么直接放回path+1。path代表路径长度。

在这里插入图片描述

  程序如下

// 127、单词接龙-深度优先搜索
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordSet(wordList.begin(), wordList.end());	// 转成uset类型,查找更快if (wordSet.find(endWord) == wordSet.end()) return 0;	// endWord没有在单词集合中出现,直接返回0unordered_map<string, int> visitMap;	// 记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度queue<string> que;		visitMap.insert(pair<string, int>(beginWord, 1));que.push(beginWord);while (!que.empty()) {string word = que.front();que.pop();int path = visitMap[word];	// 路径长度for (int i = 0; i < word.size(); i++) {string newWord = word;		// 用一个新单词替换word,每次置换一个字母for (int j = 0; j < 26; j++) {newWord[i] = j + 'a';if (newWord == endWord) return path + 1;	// 找到endif (wordSet.find(newWord) != wordSet.end() && visitMap.find(newWord) == visitMap.end()) {	// newWord出现在wordSet中,且没有访问过visitMap.insert(pair<string, int>(newWord, path + 1));que.push(newWord);}}}}return 0;}
};

复杂度分析:

  • 时间复杂度: O ( N × C ) O(N \times C) O(N×C),N为wordList长度,C为单词长度。字符串数组转化成umap字符串类型需要 O ( N ) O(N) O(N)。最坏情况下,遍历到wordList最后一个元素才会找到endWord,while循环中的一些操作(如visitMap插入元素和que队列插入、弹出元素复杂度)为 O ( N ) O(N) O(N)。两个for循环的复杂度为 O ( 26 × C ) O(26 \times C) O(26×C)。因此最终的复杂度为 O ( N × C ) O(N \times C) O(N×C)

  • 空间复杂度: O ( N × C ) O(N \times C) O(N×C)

三、完整代码

// 127、单词接龙-深度优先搜索
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordSet(wordList.begin(), wordList.end());	// 转成uset类型,查找更快if (wordSet.find(endWord) == wordSet.end()) return 0;	// endWord没有在单词集合中出现,直接返回0unordered_map<string, int> visitMap;	// 记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度queue<string> que;		visitMap.insert(pair<string, int>(beginWord, 1));que.push(beginWord);while (!que.empty()) {string word = que.front();que.pop();int path = visitMap[word];	// 路径长度for (int i = 0; i < word.size(); i++) {string newWord = word;		// 用一个新单词替换word,每次置换一个字母for (int j = 0; j < 26; j++) {newWord[i] = j + 'a';if (newWord == endWord) return path + 1;	// 找到endif (wordSet.find(newWord) != wordSet.end() && visitMap.find(newWord) == visitMap.end()) {	// newWord出现在wordSet中,且没有访问过visitMap.insert(pair<string, int>(newWord, path + 1));que.push(newWord);}}}}return 0;}
};

end

http://www.lryc.cn/news/305641.html

相关文章:

  • CAN——创建一个数据库DBC文件
  • (十三)【Jmeter】线程(Threads(Users))之tearDown 线程组
  • MySQL数据库基础(十三):关系型数据库三范式介绍
  • 掌控互联网脉络:深入解析边界网关协议(BGP)的力量与挑战
  • Vue2页面转化为Vue3
  • 【课程作业】提取图中苹果的面积、周长和最小外接矩形的python、matlab和c++代码
  • 解决easyExcel模板填充时转义字符\{xxx\}失效
  • 在项目中使用CancelToken选择性取消Axios请求
  • [c++] 记录一次引用使用不当导致的 bug
  • 能不能节约百分之九十的算力来训练模型
  • LeetCode206: 反转链表.
  • 高级统计方法 第1次作业
  • spinalhdl,vivado,fpga
  • Tomcat线程池原理(下篇:工作原理)
  • 【服务器数据恢复】通过reed-solomon算法恢复raid6数据的案例
  • LeetCode 2583.二叉树中的第 K 大层和:层序遍历 + 排序
  • element ui 安装 简易过程 已解决
  • websoket
  • 案例:微服务从Java/SpringBoot迁移到Golan
  • 小波变换模拟
  • cv::Mat图像操作
  • 【机器学习基础】一元线性回归(适合初学者的保姆级文章)
  • 2024年软件测试岗位-面试
  • 【坑】Spring Boot整合MyBatis,一级缓存失效
  • J7 - 对于ResNeXt-50算法的思考
  • R3F(React Three Fiber)基础篇
  • torch\tensorflow在大语言模型LLM中的作用
  • 设计模式-创建型模式-单例模式
  • 备战蓝桥杯—— 双指针技巧巧答链表1
  • 微信小程序返回上一级页面并自动刷新数据