Leetcode Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
注意题目意思是:给你一个单词,每次只能改变一个字母,改变后的单词要在dict中,最少经过过少次改变可以变成给定单词
本题通过广度搜索进行搜索
如
hit
经过一次改变 hot (其他单词都不在dict中,此时将hot从dict删除,这样可以避免hot->hot的循环)
经过第二次改变 dot lot (将dot和lot从dict中删除,dict={"dog","log"})
经过第三次改变 dog log
经过第四次改变 cog (找到,注意start算一次)
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
int res = ;
queue<string> que;
que.push(start);
bool flag = false;
while(!que.empty() && !flag){
int cnt = que.size();
res++;
while(cnt-->){
string a = que.front();que.pop();
if(a == end) {flag = true;break;}
for(int i = ; i < a.length();++ i){
string bk(a);
for(char j ='a' ; j <= 'z'; ++ j ){
bk[i] = j;
if(dict.find(bk)!=dict.end() && bk!=a){
que.push(bk);
dict.erase(bk);
}
}
}
}
}
if(!flag) return ;
else return res;
}
};
Leetcode Word Ladder的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [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 II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
随机推荐
- (转)CPU Cache与内存对齐
转自:http://blog.csdn.net/zhang_shuai_2011/article/details/38119657 原文如下: 一. CacheCache一般来说,需要关心以下几个方面 ...
- Excel 函数VLOOKUP初学者使用指南
1.基础说明 =VLOOKUP(lookup_value,tabble_array,col_index_num,(range_lookup)) lookup_value:用什么查找 tabble_ar ...
- 墙裂推荐一本案例驱动的PhoneGap入门书,早看早收货
清华大学出版社推出的<构建跨平台APP:PhoneGap移动应用实战> 零门槛学APP开发 从无到有 循序渐进 20余个示例APP 3个项目APP 全平台à跨终端à移动开发 完美生命周期: ...
- 【荐】MongoDB基本命令大全
DB Shell数据操作 shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的. #数据库 操作 1.Help查看命令提示 > h ...
- Shell入门教程:Shell函数详解
Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.函数定义 ...
- 支持向量机SVM
SVM(Support Vector Machine)有监督的机器学习方法,可以做分类也可以做回归.SVM把分类问题转化为寻找分类平面的问题,并通过最大化分类边界点距离分类平面的距离来实现分类. 有好 ...
- linux安装php & nginx
1.安装libxml2 地址:http://ftp.gnome.org/pub/GNOME/sources/libxml2/ wget http://caesar.acc.umu.se/pub/GNO ...
- 介绍一个非常好用的跨平台C++开源框架:openFrameworks
介绍一个非常好用的跨平台C++开源框架:openFrameworks 简介 首先需要说明的一点是: openFrameworks 设计的初衷不是为计算机专业人士准备的, 而是为艺术专业人士准备的, 就 ...
- jquery版小型婚礼(可动态添加祝福语)
前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久……一直没找到满意的……无意间看到祝福语和一些卡通婚礼图片.最 ...
- PHP 图片处理工具类(添加水印与生成缩略图)
=================ImageTool.class.php================= <?php class ImageTool { private $imagePath; ...