题目链接

Word Ladder - LeetCode

注意点

  • 每一个变化的字母都要在wordList中

解法

解法一:bfs。类似走迷宫,有26个方向(即26个字母),起点是beginWord,终点是endWord。每一次清空完队列ret+1表示走了一步。bfs可以保证是最短路径。

class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(),wordList.end());
if(!wordSet.count(endWord)) return 0;
queue<string> q;
q.push(beginWord);
int ret = 0;
while(!q.empty())
{
int size = q.size();
for(int i = 0;i < size;i++)
{
string word = q.front();q.pop();
if(word == endWord) return ret+1;
for(int j = 0;j < word.size();j++)
{
string newWord = word;
for(char ch = 'a';ch <= 'z';ch++)
{
newWord[j] = ch;
if(wordSet.count(newWord) && newWord != word)
{
q.push(newWord);
wordSet.erase(newWord);
}
}
}
}
ret++;
}
return 0;
}
};

小结

  • 不看别人的解题报告真想不到是迷宫问题

Word Ladder - LeetCode的更多相关文章

  1. Word Ladder——LeetCode

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  2. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  3. [LeetCode] Word Ladder 词语阶梯

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

  4. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  6. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  7. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  8. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  9. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

随机推荐

  1. zookeepeer集群搭建

    一.预备工作 1.zookeepeer需要安装JDK,至于版本,大家可以去官网查询一下.这里我安装的是JDK8. 2.需要开放zookeepeer用到的端口,默认端口2181.2888.3888,至于 ...

  2. MVC4程序运行报错

    近期了解MVC4的时候弄了一个简单的小工程,使用Entity Framework作为Model,F5启动调试运行的时候没有问题,但是发布到IIS之后访问就报错 错误信息如下: The Entity F ...

  3. 11.10 (下午)开课二个月零六天(ajax验证用户名,ajax调数据库)

    用ajax验证用户名是否可用 testuid.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  4. Linux系统本地yum源环境配置记录

    由于IDC的一些服务器没有外网,不能对外访问.所以打算部署一套内网的yum源环境,以供内网服务器使用.以下简单记录下操作过程: 1)下载centos6.9和centos7.3的镜像,并挂载 [root ...

  5. Centos6.9下安装并使用VNC的操作记录

    VNC是一个的"远程桌面"工具.,通常用于“图形界面”的方式登录服务器,可视化操作.废话不多说了,操作记录如下: 1)安装桌面环境 [root@vm01 ~]# yum -y gr ...

  6. C_数据结构_走迷宫

    #include <stdio.h> #include <conio.h> #include <windows.h> #include <time.h> ...

  7. C. Ehab and a 2-operation task

    链接 [https://codeforces.com/contest/1088/problem/C] 题意 n个数,最多n+1操作,要么前i个数加x,要么前i个数对x取余,最后使得严格递增 分析 直接 ...

  8. Linux内核分析 读书笔记 (第七章)

    第七章 链接 1.链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(或被拷贝)到存储器并执行. 2.链接可以执行于编译时,也就是在源代码被翻译成机器代码时:也可以执行于 ...

  9. <<浪潮之巅>>阅读笔记一

    第一次的阅读就想读这本书的,却因为很多愿意一直拖到现在,因为听说这本书在李开复先生 的微博上有推荐,更是增加了我的阅读兴趣.可能是因为在网上找的电子版的原因,通篇阅读的速度很快,但是没有纸质数那种细嚼 ...

  10. 总结 推广app

    扫一扫二维码即可安装使用我们的app,方便快捷. 电脑端下载地址:http://pan.baidu.com/s/1bocWPPX http://a.app.qq.com/o/simple.jsp?pk ...