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 length5.

Note:

Return 0 if there is no such transformation sequence. All words have the same length. All words contain only lowercase alphabetic characters.

C++

class Solution {
bool diff(string a,string b){
int c=0;
for(int i=0;i<a.size();i++)
if(a[i]!=b[i]) c++;
if(c==1) return true;
return false;
} public: int ladderLength(string start,string end,unordered_set<string> &dict){
dict.insert(end);
dict.erase(start);
queue<string> q;
q.push(start);
int length=0;
while(q.size()>0){
length++;
int QueueLength=q.size();
for(int i=0;i<QueueLength;i++){
start=q.front();
q.pop();
if(start==end) return length;
for(unordered_set<string >::iterator iter=dict.begin();iter!= dict.end();){
if(diff(start,*iter)){
q.push(*iter);
dict.erase(iter++);
}else iter++;
}
}
}
return 0;
} int ladderLength2(string start, string ends, unordered_set<string> &dict) {
int res=1;
queue<string> q;
q.push(start);
while(!q.empty()){
int size=q.size();
while(size>0){
string top=q.front();
q.pop();
size--;
if(diff(top,ends)) return res+1;
for(unordered_set<string >::iterator i =dict.begin();i!=dict.end();){
if(diff(*i,top)){
q.push(*i);
dict.erase(i++);
}else i++;
}
}
res++;
}
return 0;
}
};

word-ladder leetcoder C++的更多相关文章

  1. [LeetCode] Word Ladder 词语阶梯

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

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

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

  3. LeetCode:Word Ladder I II

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

  4. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  5. 【leetcode】Word Ladder II

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

  6. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. [Leetcode][JAVA] Word Ladder II

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

  8. LeetCode127:Word Ladder II

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

  9. 【LeetCode OJ】Word Ladder II

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

  10. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

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

随机推荐

  1. Docker Command and Dockerfile

    镜像相关命令 # 下载镜像 docker pull xxx # 搜素镜像 docker search xxx # 查看已经下载了哪些镜像 docker images # 查看已下载镜像的id dock ...

  2. 学习PHP中的信息格式化操作

    在国际化组件的学习过程中,我们已经接触过了 NumberFormatter 这种数字的格式化操作,它可以让我们将数字转换成标准格式.货币.本地语言等形式.今天我们来学习的是另一种专门用于信息格式化的类 ...

  3. 动态查看及加载PHP扩展

    在编译并完成 php.ini 的配置之后,我们就成功的安装了一个 PHP 的扩展.不过, PHP 也为我们提供了两个在动态运行期间可以查看扩展状态以及加载未在 php.ini 中进行配置的扩展的函数. ...

  4. 判断手机浏览器还是微信浏览器(PHP)

    //判断是否 微信浏览器 function isWeixin1() { if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== fa ...

  5. 『GoLang』面向对象

    我们总结一下前面看到的:Go 没有类,而是松耦合的类型.方法对接口的实现. 面向对象语言最重要的三个方面分别是:封装,继承和多态,在 Go 中它们是怎样表现的呢? Go实现面向对象的两个关键是stru ...

  6. 千位分隔符的JS实现

    $.extend({ //千位分割符 MoneySeparator: function numFormat(num){ if(num==null){ return num; }else { num=n ...

  7. TypeScript 条件类型精读与实践

    在大多数程序中,我们必须根据输入做出决策.TypeScript 也不例外,使用条件类型可以描述输入类型与输出类型之间的关系. 本文同步首发在个人博客中,欢迎订阅.交流. 用于条件判断时的 extend ...

  8. video 适配通屏展示、针对不同分辨率 禁止变形处理

    CSS object-fit 属性 object-fit: fill|contain|cover|scale-down|none|initial|inherit; 样式上 video{ height: ...

  9. 数据库的高可用MHA实验步骤

    一.多机互信的步骤 双机互信的步骤 第一步:在master管理服务器上ssh-keygen 在master同一台管理服务器上重新开一个窗口ssh-copy-id 192.168.0.13自己给自己互信 ...

  10. 数据库InnoDB和MyISAMYSQL的区别

    1.nnoDB支持事务,MyISAM不支持,这一点是非常之重要.事务是一种高级的处理方式,如在一些列增删改中只要哪个出错还可以回滚还原,而MyISAM就不可以了. 2.MyISAM适合查询以及插入为主 ...