题目链接

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. Spring Aop: 关于继承和execution target this @annotation

    1.多态 target指通过这个对象调用的方法   (匹配标识对象的所有方法)  getMethod() this指调用这个对象的方法 (匹配标识对象实现的方法) getDeclaredMethod( ...

  2. Intellij IDEA的下载和使用(针对学生的免费使用计划)

    一.下载和使用授权(针对学生) 1.下载 可以在Intellij IDEA官网上下载需要的版本.下载地址:https://www.jetbrains.com/idea/ 2.学生免费试用 首先,你得现 ...

  3. Python从菜鸟到高手(2):清空Python控制台

    执行python命令会进入Python控制台.在Python控制台中可以用交互的方式执行Python语句.也就是执行一行Python语句,会立刻返回执行结果.   当Python控制台输入过多的Pyt ...

  4. Linux下rsyslog日志收集服务环境部署记录

    rsyslog 可以理解为多线程增强版的syslog. 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL.PostgreSQL.Oracle等).日志内容筛选.定义日志格式模板等.目 ...

  5. 忘记mysql数据库root密码

    找到配置文件my.ini  ,然后将其打开,可以选择用记事本打开,查找的方法如下: 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tables,保存退出. PS: ...

  6. 作用域&作用域链和with,catch语句&闭包

    作用域(函数) 作用域:变量与函数的可访问范围,即作用域控制着变量与函数的可见性和生命周期; 在一些类C编程语言中花括号内的每一段代码都有各自的作用域,而且变量在声明它们的代码段外是不可见的,称之为块 ...

  7. Leetcode——66.加一

    @author: ZZQ @software: PyCharm @file: leetcode66_加一.py @time: 2018/11/29 16:07 要求:给定一个由整数组成的非空数组所表示 ...

  8. 发布阶段 github和360移动助手及总结

    经过一系列的冲刺和加工 最激动人心的无非在发布平台上公布上自己辛苦奋斗了一个周期的产品,这个时候的我们就像Iphone 6发布会上得CEO,为自己的产品完美画上了研发的句号. 接下来的日子就是准备ve ...

  9. [福大软工] Z班——Alpha现场答辩情况汇总

    Alpha现场答辩 小组互评(文字版) 各组对于 麻瓜制造者 的评价与建议 队伍名 评价与建议 *** 界面较友好,安全性不足,功能基本完整.希望能留下卖家的联系方式而不是在APP上直接联系,APP上 ...

  10. Apache+php安装和配置 windows

    Apache+php安装和配置 windows Apache 安装 1.官网网址:http://httpd.apache.org/ 2.Download 3.点击链接Files for Microso ...