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. python中字典按键、值进行排序

    看到排序,就不禁想起python中的sort和sorted sort是列表中的方法,用于对列表进行排序(改变的是原列表,不返回新列表) 用法: list.sort(key=None,reverse=T ...

  2. 机器学习——最优化问题:拉格朗日乘子法、KKT条件以及对偶问题

    1 前言 拉格朗日乘子法(Lagrange Multiplier)  和 KKT(Karush-Kuhn-Tucker)  条件是求解约束优化问题的重要方法,在有等式约束时使用拉格朗日乘子法,在有不等 ...

  3. 【PHP数据结构】栈和队列的应用

    通过栈和队列的学习,我们似乎会感觉到其实数据结构还是非常简单的嘛.当然,这只是一个开始,我们从顺序表.链表开始,到现在的栈和队列,其实都是为了将来在铺路.在树和图的遍历算法中,都可以见到栈和队列的身影 ...

  4. 深入HTML5第一天

    页面的title一般是30-40个字符:分别为主页,详情页,列表页  keywords:100个字符  description: em是:emphasize: 强调,着重  i:italic斜体的 : ...

  5. django 各项配置基本设置

    setting中一些设置例子 mysql数据库连接设置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': ...

  6. 远程连接centos7中mysql8.0

    远程连接centos7中mysql8.0 1.使用Navicat for MySQL或者其它数据连接软件 2.先检查centos中防火墙是否关闭,如果关闭不需要设置,如果没有关闭防火墙,请打开3306 ...

  7. redis小结 1-1

    1.1什么是resis Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 1.2Redis 与其他 key - value 缓存产品有以下三个特点 Redis支持 ...

  8. LR集合点策略

    给大家分享一个LR集合点策略,跑并发脚本时,一定要设置策略,要不然得出的响应时间无意义.默认选择第一个(当所有虚拟用户中的x % 到达集合点进释放,即仅当指定百分比的虚拟用户到达集合点时,才释放虚拟用 ...

  9. PolarDB PostgreSQL 快速入门

    什么是PolarDB PostgreSQL PolarDB PostgreSQL(下文简称为PolarDB)是一款阿里云自主研发的云原生数据库产品,100%兼容PostgreSQL,采用基于Share ...

  10. P4234-最小差值生成树【LCT】

    正题 题目链接:https://www.luogu.com.cn/problem/P4234 题目大意 给出\(n\)个点\(m\)条边的一张图.求一棵生成树使得最大边权减去最小边权最小. \(1\l ...