题目:

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. 强大易用的日期和时间库 Joda Time

    Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成,并且它是线程安全 ...

  2. 用C语言解决迷宫问题

    #include <stdio.h> #include <stdlib.h> #define ROW 10 #define COL 10 /*迷宫中位置信息*/ typedef ...

  3. JOB Hunting 总结-----2013-11-5

    从9月份开始的找工作大战,告一段落:其实早在10月中旬就已搞定,现在回想起这过去的几个月,很充实,很疲惫,很挫败又很有成就感!      开始找工作,对未来有过很多憧憬,也很迷茫,不知道自己的未来会在 ...

  4. 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

    错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...

  5. HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程

    在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...

  6. vs2013数据库连接对应的dll

    mysql for visual studio 1.1.1mysql connector net 6.3.9mysql connector/odbc 5.3

  7. 短信计时器Utils

    package com.lvshandian.partylive.utils; import android.content.Context;import android.os.CountDownTi ...

  8. JS基础知识再整理..........不断更新中

    1.JS的五种基本数据类型:字符串.数值.布尔.null.underfined. 2.在JS中,字符串.数值.布尔三种数据类型,有其属性和方法: 3.字符串的三种常用方法[.indexof()..su ...

  9. HibernateTemplate方法的使用

    1.查询帖子(Post)为例 查找所有的帖子 public List<Post> findPosts() { String hql = "from Post p left joi ...

  10. await 暂停 等待 暂停的是什么

    体验异步的终极解决方案-ES7的Async/Await var sleep = function (time) { return new Promise(function (resolve, reje ...