题目:

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

题解:

  BFS

Solution 1 ()

class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if (start == end) {
return ;
}
int n = start.size();
if (n < || n != end.size()) {
return ;
} queue<string> q;
q.push(start);
dict.erase(start);
int length = ; while (!q.empty()) {
int size = q.size();
for (int i = ; i < size; ++i) {
string cur = q.front();
q.pop();
for (int j = ; j < n; ++j) {
char oldChar = cur[j];
for (char c = 'a'; c <= 'z'; ++c) {
if (c == oldChar) {
continue;
}
cur[j] = c;
if (cur == end) {
return length;
}
if (dict.find(cur) != dict.end()) {
q.push(cur);
dict.erase(cur);
}
}
cur[j] = oldChar;
}
}
++length;
}
return ;
}
};

【Lintcode】120.Word Ladder的更多相关文章

  1. 【LeetCode】127. Word Ladder

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

  2. 【leetcode】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

  3. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  4. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  5. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  8. 论文笔记【四】Semi-supervised Word Sense Disambiguation with Neural Models

    基于神经模型的半监督词义消歧 Dayu Yuan  Julian Richardson  Ryan Doherty  Colin Evans  Eric Altendorf Google, Mount ...

  9. 【转】给word中的代码着色

    基本操作 1)用Notepad++直接编辑代码文件,注意文件后缀,比如.cpp是C++程序,.m是Matlab,linux文件是.sh,写对后缀表示的文件类型,才有对应的语法高亮效果. 2)选中需要的 ...

随机推荐

  1. redis远程登录

    #redis-cli -h 202.96.126.37 -p 6379 #auth 'd6d72fa9b2ff458e:GRjZmQ3MTN'

  2. cmake学习之- cmake_parse_arguments

    最后更新: 2019-06-08 一.指令介绍 cmake_parse_arguments 为解析函数(function)或 宏(macros) 参数的命令: cmake_parse_argument ...

  3. [Linux] 概念

    操作系统包括: 内核:管理硬件资源 库:没有执行入口的程序,用于提升软件开发效率 应用程序:有执行入口的程序 常见库文件: windows系统:dll(dynamic link library)动态链 ...

  4. Smarty入门学习

    --------------------------------- 安装和设置 --------------------------------- require('../Smarty/Smarty. ...

  5. Grunt学习笔记【5】---- expand使用方法

    本文主要讲expand使用方法. 当你希望处理大量的单个文件时,这里有一些附加的属性可以用来动态的构建一个文件列表.这些属性都可以用于 Compact 和 Files Array 文件映射格式. ex ...

  6. wxPython的Refresh与事件双重响应

    #!/usr/bin/env python import wx class DoubleEventFrame(wx.Frame): def __init__(self, parent, id): wx ...

  7. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  8. 网页布局的应用(float或absolute)

    一个浮动(左浮动或右浮动) 垂直环绕布局(float.clear) 左右两列布局(float.absolute) 三栏网页宽度自适应布局(float.absolute) 注意:网页设计中应该尽量避免使 ...

  9. SDUT OJ I样(0-1背包问题 【模板】)

    I样 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 这是个什么问题呢?DP,贪心,数据结构,图论,数论还是计算几何?管他呢,反正 ...

  10. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...