Word Ladder

Word Ladder/ Word Ladder II 题目分析 1. Word Ladder和Word Ladder II两题是连续的,基本是同一个问题的两个难度,因此放在一起做了。 2. Word Ladder是要找出一条从Start Word 到 End Word的最短路径,一个比较简单的想法就是建立一张图,描述每个在字典中的词之间的可达性,只有任意两个词间有且仅有一个字母不同的时候是直接可达的,然后用BFS的方式去找到Start Word和 阅读详情

Given two words (beginWord and endWord), and a dictionary, 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 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 length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

题意很容易看懂,所用的方法是广度优先搜索(BFS)算法。


int diffCount(const string& s1, const string& s2)
{
	int count = 0, len = s1.length();
	for (int i = 0; i < len; i++)
	{
		if (s1[i] != s2[i])
			count++;
	}
	return count;
}
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict)
{
	if (wordDict.size() == 0)
		return 0;

	queue<string> q;
	q.push(beginWord);
	unordered_map<string, int> visitLevel;//访问层次,每个字符串对应各自的最短访问长度
	visitLevel[beginWord] = 1;

	while (!q.empty())
	{
		string word = q.front(); q.pop();
		for (unordered_set<string>::iterator it = wordDict.begin(); it != wordDict.end(); )
		{
			string ladderWord = *it;
			if (diffCount(word, ladderWord) == 1)	//word是否可以通过一次变更就达到下一个词典里有的某个单词
			{
				visitLevel[ladderWord] = visitLevel[word] + 1;//step+1
				if (*it == endWord)
					return visitLevel[ladderWord];
				
				q.push(ladderWord);		//加入队列
				it = wordDict.erase(it);//从词典中删除访问过的单词
			}
		}
	}

	return 0;
}


图解LeetCode:Word Ladder 文章目录思考过程建立索引代码实现总结 这是LeetCode的第127号题目Word Ladder,也就是单词梯子,那这个题目是什么意思呢? 给定两个单词,一个为开头单词beginWord,一个为结尾单词endWord,并且给定一个单词列表,找出从开头单词到结尾单词的最短转换路径,转换规则是这样的: 一次只能转换单词中的一个字符 每次转换后的单词必须存在于单词列表中 比如给定开头单词“hit”,... 阅读详情

相关推荐

LeetCode126—Word Ladder II

LeetCode126—Word Ladder II

上善若水 4910

LeetCode127—Word Ladder

LeetCode127—Word Ladder

上善若水 2702

DeerFlow 在 Windows 上的安装与启动详细步骤

DeerFlow Windows安装与启动指南 DeerFlow是基于LangGraph的AI Agent系统,包含后端服务和前端界面。后端安装需Python 3.12+,创建虚拟环境后安装依赖,需注意解决langgraph版本冲突问题(推荐1.0.8版本)。配置文件中需设置模型参数(如本地Qwen3-32B-AWQ模型)和sandbox配置。启动分为两步:LangGraph Server(端口2024)和Backend Gateway(端口8001)。前端要求Node.js 22+和pnpm,配置.env

笨小孩 581

LeetCode——Word Ladder II

链接:http://leetcode.com/onlinejudge#question_126 原题: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one

niaokedaoren的专栏 8948

FineReport SQL查询分页

打开模板>模板web属性>分页预览设置,选择为该模板单独设置,将默认的工具栏按钮全部清除掉,然后依次添加两个自定义按钮,一个邮件按钮,两个自定义按钮,一个Word格式,一个PDF按钮。如下所示:注:使用具体格式按钮,如邮件、Word格式、PDF按钮是为了JS直接使用控件名称获取空间方便修改其内容;

lep的博客 2427

IPC-7351标准实战:如何用LPWizard_10快速生成PCB封装库(附避坑指南)

本文提供了一份详细的IPC-7351标准实战指南,手把手教你使用LPWizard_10软件快速生成符合行业标准的PCB封装库,并直接导入Cadence Allegro。文章深入解读了IPC-7351密度等级的选择逻辑,并重点分享了软件配置、QFN封装生成实战步骤,以及导出失败、焊盘异常等常见问题的避坑解决方案,旨在帮助硬件工程师提升设计效率和可靠性。

weixin_29207439的博客 153

leetcode之word-ladder

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: Only one letter can be changed at a time Ea...

此心光明,亦复何言~ 529

CH569 USBSS库函数分析

CH569 USB3.0 解析

havc_W的博客 7683

[Lintcode]120. Word Ladder /[Leetcode]127. Word Ladder

120. Word Ladder / 127. Word Ladder 本题难度: Hard/Medium Topic: Data Structure Description Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from sta...

weixin_30932215的博客 150

【论文复现】——基于欧式聚类的车载点云道路提取

针对车载激光点云道路信息提取的问题,提出了一种基于欧式聚类的道路提取方法。首先通过统计滤波去除噪点以免对后续处理结果产生影响,然后用坡度滤波去除路坎点,使道路边界与人行道、绿化带等与路坎相连的地物形成10~25 cm的高程阶跃,最后用欧式聚类将路面点云与其他地物点云聚类为一簇或多簇并保留道路簇,从而实现道路点云的精确提取。

点云侠的博客 1134

OpenClaw第2章:快速安装

本章将手把手教你安装 OpenClaw。

Maynor的博客 3236

Leetcode 127 Word Ladder I

这道题目推荐和Leetcode 102 Binary Tree Level Order Traversal(http://blog.youkuaiyun.com/zxzxy1988/article/details/8597354)连起来看 这道题目首先想到的是DFS,或曰backtracking,也就是每次都找到一个可能的路径,最后比较所有路径中最小的就是题目所求。这样做显然需要较多的时间,因为我们

思维决定一切 1万+

SoulX-FlashHead 加速实践,提升3倍。

SoulX-FlashHead 加速实践,提升3倍。

jacke121的专栏 220

LeetCode 127. Word Ladder

127. Word Ladder 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: Only one letter ca

柳婼 の blog 1049

Bev算法在J5平台的部署

支持多种形式的Bev算法:ipmlss时序,以及基于transformer的gkt。

@bangbang的博客 845

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[LeetCode]Word Ladder II

WordLadder I 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: Only one lette

yy254117440的博客 451

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

[leecode]Word Ladder II

Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate...

mc46790090的专栏 178

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

leetcode 126. Word Ladder II

leetcode 126. Word Ladder II   写这一题 真的非常适合复习bfs与dfs啊 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.ut

独钓寒江雪 559

leetcode Word Ladder II

和上题Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能。 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头。所以不能立马删除。但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknow...

214

Word Ladder II -- LeetCode

Word Ladder II -- LeetCode

qq_44939824的博客 139

word ladder

[leetcode]Word Ladder II @ Python [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.youkuaiyun.com/doc_sgl/article/details/13341405       

screaming的博客 391

LeetCode127. Word Ladder

127. Word Ladder 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: Only one letter can b...

grllery的博客 436

LeetCode-126. Word Ladder II [C++][Java]

Given two words,beginWordandendWord, and a dictionarywordList, returnall theshortest transformation sequencesfrombeginWordtoendWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words[beginWord,

贫道绝缘子的博客 1131
上一篇: LeetCode Word Break
下一篇: Spark streaming 初探
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值