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.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

思路:

字符串的花儿挺多,word ladder也是CC150 v5上的18.10题,乍一看无从下手,其实就是BFS求单源无权最短路径。

想要大set不超时,关键有几点:

1. 一边加新词进queue,一边从dict里remove掉

2. 直接寻找所有可能的OneEditWords判断是否在dict里面,每次都过一遍dict一个一个判断是否为OneEditWords会超时。我还有一个超时的方法,就是寻找所有可能的OneEditWords再建一个unordered_set跟dict求交集,后来发现algoritm里的set_intersection只支持两个有序集合很坑,需要先把unordered_set转化为vector排序,结果不言而喻。

3. 将unordered_map<string, int > path并到访问队列qwords里去会跑得更快,反正这里不要求输出路径,这个可以做下优化

4. 将getOneEditWords这个函数并到ladderLength里头会跑得更快,不过我觉得可读性会降低

 int ladderLength(string start, string end, unordered_set<string> &dict) {
unordered_set<string> ndict(dict);//最好不要修改输入参数
queue<string> qwords;//BFS访问队列
unordered_map<string, int > path;//记录到start的最短路径,其实可以并入qwords中 qwords.push(start);
path[start] = ;
ndict.erase(start); while(!qwords.empty()){
start = qwords.front();
qwords.pop();
int len = path[start];
for(string s :getOneEditWords(start)){//边局部构建map,边处理
if(ndict.find(s) == ndict.end()) continue;//一个一个判断dict中元素是否为OneEditWords会超时
if(s == end) return len+;
qwords.push(s);
path[s] = len+;
ndict.erase(s);//如果不erase访问过的元素会超时
}
}
return ;
} vector<string> getOneEditWords(string start){
vector<string> words;
for(unsigned int i = ; i < start.size(); i++){
string word(start);
for(char ch = 'a'; ch <= 'z'; ch++){
if(ch != start[i]){
word[i] = ch;
words.push_back(word);
}
}
}
return words;
}

【题解】【字符串】【BFS】【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 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  8. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)

    要求最短距离.采纳dijkstra查找节点之间的最短路径. 当心:假设是一个枚举字典22是否元素可以,如果转换,暂停. 提高:每串,带您历数它的字符值事件,对于的长度n一个字符串枚举n*26次要. 设 ...

  10. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

随机推荐

  1. php header setcookie headers_sent函数 函数检查 HTTP 标头是否已被发送以及在哪里被发送

    这里需要注意的 header() 最常被拿來送 header('Location: /'); 等等, 做网页重定向的动作. 在使用 setcookie(), header()... 等函数前 不可以用 ...

  2. LCA-倍增法(在线)

    原文:http://www.tuicool.com/articles/N7jQV32 1. DFS预处理出所有节点的深度和父节点 inline void dfs(int u) { int i; for ...

  3. comboBox的id返回System.Data.DataRowView

    关系到ComboBox的DataSource,DisplayMember和ValueMember属性的设置顺序的问题. ComboBox的DataSource属性为object类型,但是需要实现ILi ...

  4. Scss sass

    http://www.ruanyifeng.com/blog/2012/06/sass.htmlscss 声明:1,$blue : #1875e7;2,.class1 { border: 1px so ...

  5. NodeJs编写小爬虫

    一,爬虫及Robots协议 爬虫,是一种自动获取网页内容的程序.是搜索引擎的重要组成部分,因此搜索引擎优化很大程度上就是针对爬虫而做出的优化. robots.txt是一个文本文件,robots是一个协 ...

  6. IT公司100题-3-求数组的最大子序列的和

    问题描述: 输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2 ...

  7. mysql 启动错误1026

    进入“事件查看器”“应用程序”果然发现很多MySql的错误Default storage engine (InnoDB) is not available 于是进入MySql的安装目录找到my.ini ...

  8. PowerShell并发控制-命令行参数之四问

    传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...

  9. 深入理解dispatch_queue

    Grand Central Dispatch是苹果过去几年创造出来的非常强大的API,在Let's Build系列的最新一期中,我们将探究dispatch_queue基础功能的重新实现.该主题是Rob ...

  10. 在Android Studio中使用BaiduMap SDK实时获取当地位置信息

    配置BaiduMap 环境 1.在百度API中新建自己的一个APP包名和APP名需要注意和自己Android Studio 中的包名和APP名保持一致: 2.百度地图中还需要填写一个SHA1 数字签名 ...