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. Python 爬虫常用的库

    一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...

  2. HTML XHTML HTNL5 简介

    XHTML 是HTML与XML(扩展标记语言)的结合物 包含了所有与XML语法结合的HTML 4.01元素 XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup ...

  3. 中国地区免费注册bitcointalk论坛教程

    bitcointalk论坛是著名的老牌比特币论坛,中本聪当年也在这里和各路大神探讨.但现在国家的高墙禁止网民访问. 你可能会用一个国外的代理工具来看贴,看贴确实可以,但是如果想注册,注册完后就会发现帐 ...

  4. IO模型——IO多路复用机制

    (1)I/O多路复用技术通过把多个I/O的阻塞复用到同一个select.poll或epoll的阻塞上,从而使得系统在单线程的情况下可以同时处理多个客户端请求.与传统的多线程/多进程模型比,I/O多路复 ...

  5. Django本地开发,引用静态文件,火狐浏览器不能访问静态文件,谷歌浏览器却能访问静态文件

    查了一下是settings.py设置问题 # Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/1.1 ...

  6. Web阶段总结以及感受(附带大一结束暑期学习的纲要)

    之前本人大一因为不是计算机专业的,而又喜欢计算机,所以在大一临时转专业到了计算机院(费劲一番波折),冷笑,还好,从大二开始就可以正式学习喜欢的软件了. 首先,前两天看到一个讲座,提到学习方法,并说出总 ...

  7. [转][JSBSim]使用VS2015编译JSBSim

    转自csdn原文:https://blog.csdn.net/yu_lei_/article/details/81463187 请大家去看原文,原文有图片和资源,本文仅供本人参考 权威参考:http: ...

  8. 力扣(LeetCode) 849. 到最近的人的最大距离

    在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的. 至少有一个空座位,且至少有一人坐在座位上. 亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上. 返回 ...

  9. leecode第七十八题(子集)

    class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { vect ...

  10. Unity --- sharedMaterial 、material

    sharedMaterial: 无论如何更新材质的属性,内存中只会存在一份. material: 每次更新材质属性的时候,内存中都会重新new一份material作用于它,直到 Application ...