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 **的更多相关文章

  1. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  2. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  4. Java实现 LeetCode 127 单词接龙

    127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...

  5. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  6. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  7. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. [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 ...

  9. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

随机推荐

  1. JZ2440之GPIO篇

    买来开发板已经有一段时间了,刚接触时兴奋至极,后来跟着视频看下去发现似乎自己并没有学到太多东西,于是发现自己可能欠缺的太多以致从课程中无法提取出重要的东西来,所以并没有得到太多的营养成分.因此我个人认 ...

  2. UI之ECharts

    官网 效果图展示: 特性 ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Fir ...

  3. 如何让浏览器不解析html?

    原问题: 在页面中,除了xmp,textarea以及js转义外,还有什么办法可以让html标签在不被浏览器解析而正常显示呢? 答: 要符合“内部的html标签不被解析”,我们根据HTML5的标准,分元 ...

  4. 基于 Python 和 Pandas 的数据分析(6) --- Joining and Merging

    这一节我们将看一下如何通过 join 和 merge 来合并 dataframe. import pandas as pd df1 = pd.DataFrame({'HPI':[80,85,88,85 ...

  5. 关于python的面向对象

    一,面向对象 1..面向对象的过程:一切以事物的流程为核心,核心是过程二字,过程是指解决问题的步骤, 是一种机械是的编程思维 优点:负责的问题流程化,编写相对简单 缺点:可扩展性能差 2.面向对象一切 ...

  6. 性能测试 Performance Test Report

    时间隔了很久,两年左右了吧,最近打开原来的测试报告,测试数据还是很漂亮的.TPS比我记忆中的要高很多. 数据中有些是定死了的(当时的要求),并发不是计算的,是用几个值跑起来试试看的.因为后期我们会用S ...

  7. 使用openpyxl实现excel文件的读取操作

    1.环境准备 python3环境.安装openpyxl模块 2.excel文件数据准备 3.为方便直接调用,本代码直接封装成类 from openpyxl import load_workbook c ...

  8. 第 7 章 多主机管理 - 045 - 安装 Docker Machine

    安装 Docker Machine 先安装docker 官方安装docker-machine的文档地址:https://docs.docker.com/machine/install-machine/ ...

  9. 第 3 章 镜像 - 010 - base 镜像

    base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 能称作 base 镜像的通常都是各种 Linux 发行版的 Docker 镜像,比如 Ubunt ...

  10. 获取解码字符串指定位置的数值 Decoded String at Index

    2018-10-04 12:53:06 问题描述: 问题求解: 首先本题给出了问题的规模,从Note中我们可以看到解码后的字符串长度甚至可以达到2^63的长度,这个长度已经远远超过整型数的范围,因此如 ...