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. template render in javascript

    art-template for github 中文官方文档

  2. 转载:mysql存储过程讲解

    记录MYSQL存储过程中的关键语法: DELIMITER // 声明语句结束符,用于区分; CEATE PROCEDURE demo_in_parameter(IN p_in int) 声明存储过程 ...

  3. PTA 7-2 列车调度(25 分)

    7-2 列车调度(25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道 ...

  4. cmake用法及常用命令总结

    CMakeLists.txt 的语法比较简单,由命令.注释和空格组成,其中命令是不区分大小写的.指令是大小写无关的,参数和变量是大小写相关的.但推荐全部使用大写指令.符号 # 后面的内容被认为是注释. ...

  5. Mysql 查看定时器 打开定时器 设置定时器时间

    1.查看是否开启evevt与开启evevt. 1.1.MySQL evevt功能默认是关闭的,可以使用下面的语句来看evevt的状态,如果是OFF或者0,表示是关闭的. show VARIABLES ...

  6. Java——文件及目录File操作

    API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...

  7. UT, FT ,E2E 测试的意思

    前端实现自动化就要借助到unit和e2e端到端测试了 一.unit测试(FT 就是Fucntion Test 功能测试,  注意不是: funciton函数 ...fucntion功能   不一样哦  ...

  8. Android打开相机进行人脸识别,使用虹软人脸识别引擎

    上一张效果图,渣画质,能看就好 功能说明: 人脸识别使用的是虹软的FreeSDK,包含人脸追踪,人脸检测,人脸识别,年龄.性别检测功能,其中本demo只使用了FT和FR(人脸追踪和人脸识别),封装了开 ...

  9. BootStrap table隐藏列两种方式 (踩坑)

    1.第一种  利用 visible 属性 { field : 'userAccount', title : '订阅人', visible : visible(), formatter:function ...

  10. 解开lambda最强作用的神秘面纱

    我们期待了很久lambda为java带来闭包的概念,但是如果我们不在集合中使用它的话,就损失了很大价值.现有接口迁移成为lambda风格的问题已经通过default methods解决了,在这篇文章将 ...