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. FastAPI(4)- get 请求 - 路径参数 Path Parameters

    什么是路径 假设一个 url 是: http://127.0.0.1:8080/items/abcd 那么路径 path 就是 /items/abcd 路径参数 就是将路径上的某一部分变成参数,可通过 ...

  2. 洛谷P1781——宇宙总统(高精度排序)

    题目描述 地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竟选总统,现在票数已经统计完毕,请你算出谁能够当上总统. 输入输出格式 输入格式: 第一行为一个整数n,代表竞选 ...

  3. Django学习day07随堂笔记

    今日考题 """ 今日考题 1.必知必会N条都有哪些,每个都是干啥使的 2.简述神奇的双下划线查询都有哪些方法,作用是什么 3.针对多对多外键字段的增删改查方法有哪些,各 ...

  4. 简单了解Phar代码打包工具的使用

    简单了解Phar代码打包工具的使用 Phar 是在 PHP5 之后提供的一种类似于将代码打包的工具.本质上是想依照 Java 的 Jar 文件那种形式的代码包,不过本身由于 PHP 是不编译的,所以这 ...

  5. 浏览器缓存旧的js文件或css文件导致没出现预期效果

    最好在加载的js或css文件后加上 ?v=1.0.0 版本号,更新js后就更改一下版本号即可

  6. Apache设置禁止访问网站目录

    使用Apache作为Web服务器的时候,在当前目录下没有index.html|php等入口就会显示目录.让目录暴露在外面是非常危险的事. 找到Apache的配置文件 /etc/apache2/apac ...

  7. Linux系列(29) - rpm包命名规则(1)

    RPM包命名规则 例如包名:httpd-2.2.15-15.el6.centsos.1.i686.rpm 软件包名-httpd 软件版本-2.2.15 发布的次数-15 el6.centos适合的Li ...

  8. 配置 放上传文件的目录 apache(httpd)

    1. 确认服务器 开放8088端口 https://www.apachefriends.org/download.html 下载XAMPP for Windows,安装 2. 修改apache主配置文 ...

  9. html正文提取工具goose的安装及简单使用Demo

    1.git clone https://github.com/grangier/python-goose.git 2.cd python-goose 3.sudo pip install -r req ...

  10. php备份mysql 数据库

    1.新建php文件 <?phpheader('Content-Type:text/html;charset=utf8'); ini_set("max_execution_time&qu ...