Leetcode 127 **
- class Solution {
- public:
- int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
- unordered_set<string> wordset(wordList.begin(),wordList.end());
- wordset.erase(beginWord);
- int res = ;
- queue<string> que{{beginWord}};
- while(!que.empty()){
- int len = que.size();
- for(int i=;i < len;i++){//坑:int i=0;i < que.size();i++ que.size()会不停改变
- string word = que.front(); que.pop();
- if(word == endWord) return res+;
- for(int i=;i < word.size();i++){
- string newword = word;
- for(char ch = 'a';ch <= 'z';ch++){
- newword[i] = ch;
- if(wordset.count(newword)){
- que.push(newword);
- wordset.erase(newword);
- }
- }
- }
- }
- res++;
- }
- return ; //没找到
- }
- };
好题!第一次见这题还是在一年前刚学算法的时候,今日硬着头皮分析了下去,前几天写的DFS T了,我都写了DFS居然没有想到这其实是一个求图的最短跳数的题:
单词之间能够变化可以抽象为两点之间有一条有向路径,BFS找到第一个点有向路径连接的所有点加入队列,然后对队列中的点再找图中剩下直连的点,最先到达终点的距离就是层数,直接返回。
还有要把层次遍历和BFS联系起来!都是用的队列,对每“批次”的队列遍历循环,两者本质是一样的。
最后注意 que.size()的坑,之前遇到过,还好看出来了。
Leetcode 127 **的更多相关文章
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Java实现 LeetCode 127 单词接龙
127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
随机推荐
- Microsoft.EntityFrameworkCore.Sqlite的学习
SQLite in ASP.NET Core with EntityFrameworkCore ASP.NET Core 2: Using SQLite as a light weight datab ...
- 以结算价交易TAS和以市价交易TAM
CME Group的合约规格中提到TAS和TAM交易,如:Gold Futures Contract Specs Gold 期货 合约规格 Trading at Settlement (TAS) is ...
- 20165306 预备作业3 Linux安装及学习
查看了许多教程,VirtualBox和Ubuntu已安装完成.以下为学习Linux基础入门课程的实验报告.实验截图.尚未解决的问题及体会. 实验三 用户及文件权限管理 一.Linux用户管理 (一)查 ...
- javascript知识体系
JAVASCRIPT 篇 0.基础语法 javascript基础语法包括:变量定义.数据类型.循环.选择.内置对象等. 数据类型有string,number,boolean,null,undefine ...
- Android 4.0之后的日历控件拥挤的解决办法
本意是想做成这个样子的控件: 发现使用datepicker之后,效果完全不同,把整个日历都显示出来了.非常拥挤. 在datepicker中加入android:calendarViewShown=&qu ...
- 每日质量NPM包复制_copy-to-clipboard
一.copy-to-clipboard 官方定义: Simple module exposing copy function 理解: 一个超级简单的复制功能,并且这种方法适用于通过别的事件触发复制功能 ...
- 单源最短路——Dijkstra模板
算法思想: 类似最小生成树的贪心算法,从起点 v0 每次新拓展一个距离最小的点,再以这个点为中间点,更新起点到其他点的距离. 算法实现: 需要定义两个一维数组:①vis[ i ] 表示是否从源点到顶点 ...
- 【NPOI】WebAPI-使用NPOI导出Excel
1.安装nuget包 2.封装方法 public byte[] ExportToByteArray(IEnumerable<string> headerText, IEnumerable& ...
- hibernate框架入门配置
1.新建一个项目(可以是java项目,也可以是webapp) 2.导入jar包,包括日志,mysql驱动,必备request包,jpa配置规范包 3.创建实体类 1.创建数据库,使用hibernate ...
- django序列化 serializers
Django的序列化工具让你可以将Django的模型‘翻译’成其它格式的数据.通常情况下,这种其它格式的数据是基于文本的,并且用于数据交换\传输过程. 一.序列化数据 Django为我们提供了一个强大 ...