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. linux 命令行 光标移动技巧

    linux 命令行 光标移动技巧 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下面工作.但是事情也不是 ...

  2. Fedora 24 Gnome Boxes 无法ping通网络

    安装Fedora 24在试用虚拟机时发现无法ping通外网. 我傻傻地以为是软件问题. 问题描述: 尝试ping程序来测试网络连通性: (我之前也是ping百度,后来在为了少打字百度了一些比较短的域名 ...

  3. JS策略模式

    1.策略模式的定义 策略模式又叫算法簇模式,将一组算法分装到一组具体共同接口的独立类或者对象中,它不影响客户端的情况下发生变化. 通常策略模式适用于当一个应用程序需要实现一种特点的服务和功能,而且该程 ...

  4. webpackJsonp is not defined?

    用了CommonsChunkPlugin生成了公共文件,但是页面还没有引用这个公共文件 比如下面这个配置 var webpack = require('webpack'); var path = re ...

  5. [Unity] 3D数学基础 - 2D旋转矩阵

    2D矩阵的旋转: NewX = X * Cos(α) - Y * Sin(α) NewY = X * Sin(α) + Y * Cos(α) 一般在三角函数中使用的是弧度,我们可以通过下面的公式将角度 ...

  6. 在Application中集成Microsoft Translator服务之开发前准备

    第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...

  7. Bash 的 no-fork 优化

    我们知道,Bash 在执行一个外部命令时,会先 fork() 一个子进程,然后在子进程里面执行 execve() 去加载那个外部程序.fork 子进程是会耗性能的,所以 Bash 会在下面几种情况下不 ...

  8. CSS百分比定义高度的冷知识

    当我们给块级元素设置响应式高度的时候,例如给div设置height=50%,往往没能看到效果. 原因是百分比的大小是相对其父级元素宽高的大小,如最外层元素设置的百分比是对应屏幕而言的. 需要了解的是对 ...

  9. JDBC ODBC区别

    一.JDBC(Java DataBase Connectivity standard) 1.JDBC,它是一个面向对象的应用程序接口(API), 通过它可访问各类关系数据库. 2. 驱动程序(JDBC ...

  10. NOSDK--一键打包的实现(四)

    1.4 打包及签名的脚本介绍 我们使用ant来实现打包,这节我们先介绍脚本内容,关于脚本环境配置问题,我们将在下节做一个详细的介绍. 首先我们来看下build_android/tools/platfo ...