Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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的更多相关文章

  1. LeetCode:Word Ladder I II

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

  2. [leetcode]Word Ladder II @ Python

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

  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 II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  6. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  7. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  9. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

随机推荐

  1. thinkphp上传

    上传代码 // 缩略图上传 $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = ;// 设置附件上传大小 $upload-> ...

  2. rsync快速删除海量文件

    rsync --delete-before -avH --progress --stats /tmp/empty/ /var/spool/postfix/maildrop/ 由于业务侧使用时,一些脚本 ...

  3. Java类中各种静态变量的加载顺序的学习

    最近在补<thinking in java>...有一节提到了加载类需要做的一些准备...我照着书本敲了一下代码...同时稍微修改了一下书本上的代码.... package charpte ...

  4. PHP数组

    PHP数组的遍历 使用for语句循环遍历数组 在PHP中,不仅可以指定非连续的数字索引值,而且还存在以字符串为下表的关联数组.所以在php中很少使用for语句循环来遍历数组.使用for语句遍历连续数字 ...

  5. hadoop集群安装故障解决

    nodemanager进程解决:http://blog.csdn.net/baiyangfu_love/article/details/13504849 编译安装:http://blog.csdn.n ...

  6. log4net部分配置说明

    第一步: 添加并应用Log4net.dll.然后在Web.config文件中添加下面的配置局 <configSections>     <section name="log ...

  7. C#高级编程笔记 Delegate 的粗浅理解 2016年9月 13日

    Delegate [重中之重] 委托 定义一:(参考)http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 完全可 ...

  8. MiniProfiler(MiniProfiler.EF6监控调试MVC5和EF6的性能)

    git:  https://github.com/MiniProfiler 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天 ...

  9. cf593c

    题意:有n(n<=50)个圆,给出每个圆的圆心坐标和半径r(r>=2). 求两个函数f(t),g(t),t的取值为0到50的整数,每次令x=f(t),y=g(t),产生一个51个点的集合. ...

  10. 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

    Fragment newfragment =new MyFragment();fragmentTransaction.replace(R.layout.activity_main,newfragmen ...