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的更多相关文章
- Word Ladder——LeetCode
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- Word Ladder leetcode java
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- [Leetcode Week5]Word Ladder
Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...
随机推荐
- kafka学习2:kafka集群安装与配置
在前一篇:kafka学习1:kafka安装 中,我们安装了单机版的Kafka,而在实际应用中,不可能是单机版的应用,必定是以集群的方式出现.本篇介绍Kafka集群的安装过程: 一.准备工作 1.开通Z ...
- Mongo 开发笔记
时间 程序的时间是本地时间 ,数据库中的时间是 ISO 标准时间 . ISO时间 + 8 小时 = 本地时间(北京时间 ) Java驱动会自动做转化. 语法 数组查询 数据查询使用 elemMatch ...
- Mesos+Zookeeper+Marathon的Docker管理平台部署记录(2)- 负载均衡marathon-lb
之前介绍了Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)的操作,多余的废话不说了,下面接着说下在该集群环境下的负载均衡marathon-lb的部署过程: 默认情况 ...
- Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)
随着"互联网+"时代的业务增长.变化速度及大规模计算的需求,廉价的.高可扩展的分布式x86集群已成为标准解决方案,如Google已经在几千万台服务器上部署分布式系统.Docker及 ...
- 对MSF八个原则的思考
第一个原则,也是MSF中最基础的一个原则,推动信息共享与沟通.这个原则的一个特点是,对于团队成员的所有工作,都会被记录下来,包括走了弯路的.出现bug但已调试解决的部分.对于新加入团队的成员或者以前没 ...
- 第六次Scrum meeting
第六次Scrum meeting 任务及完成度: 成员 12.21 12.22 陈谋 任务1040:完成stackoverflow的数据处理后的json处理(98%) 任务1114-1:完成对网页数 ...
- 《Linux内核设计与实现》 第一二章学习笔记
<Linux内核设计与实现> 第一二章学习笔记 第一章 Linux内核简介 1.1 Unix的历史 Unix的特点 Unix很简洁,所提供的系统调用都有很明确的设计目的. Unix中一切皆 ...
- beta版使用说明
StudyAssistant说明书 我们的软件使用简单方便,下面就让我们在介绍软件界面的同时一同来介绍我们的软件使用方法: 1.这是我们软件的首页界面,单刀直入,简单明了,四科同时类课程,更好的帮助同 ...
- QT QProgressBar QProgressDialog 模态,位置设置,无边框,进度条样式
一 关于模态设置 QProgressDialog可以设置模态(需要在new的时候传入parent),QProgressBar设置不好: 只有dialog可以设置模态,widget不能设置模态(QPr ...
- PhantomJS - Scriptable Headless Browser
http://phantomjs.org/ PhantomJS is an optimal solution for: Page automation Access webpages and extr ...