【题解】【字符串】【BFS】【Leetcode】Word Ladder
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 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的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)
要求最短距离.采纳dijkstra查找节点之间的最短路径. 当心:假设是一个枚举字典22是否元素可以,如果转换,暂停. 提高:每串,带您历数它的字符值事件,对于的长度n一个字符串枚举n*26次要. 设 ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
随机推荐
- BZOJ2721 [Violet 5]樱花
先令n! = a: 1 / x + 1 / y = 1 / a => x = y * a / (y - a) 再令 k = y - a: 于是x = a + a ^ 2 / k => ...
- 项目管理办公室 PMO
项目管理办公室是组织中指导,协调,支持项目管理工作的一个常设职能部门,也就是管理项目管理的常设职能部门. 它负责指定和贯彻标准化的项目管理方法论(包括工作流程与规章制度等),协调所辖的各项目对资源,工 ...
- qml ios长按晃动
WidgetModel.qml import QtQuick 1.0 ListModel { ListElement { icon: "Images/widget1.png"; g ...
- centos 5.5 安装 lnmp
centos5.5 安装 lnmp,一定要事先选好版本安装,建议自己下载安装. 1.相关文件目录: nginx: /www/nginx/下面mysql: /usr/share/mysql /usr/b ...
- js基础之动画(一)
一.让div动起来 var oBtn = document.getElementById('btn1'); var timer='';//设置定时器 oBtn.onclick=function st ...
- 两天以来对plsqldev操作的记忆
frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...
- Debug的F5~F8用法
快捷键(F6)单步执行程序,遇到方法时跳过. 快捷键(F8)执行此断点到最后,进入下一个断点开始之处. 快捷键(F5)单步执行程序,遇到方法时进入. 快捷键(F7)单步执行程序,从当前方法跳出.
- 安装svnx2出现 Make sure an svn tool (≥ v1.6) is present in the folder: “/usr/bin”
安装svnx2出现 Make sure an svn tool (≥ v1.6) is present in the folder: “/usr/bin” 是因为svnx2需要用到svn的地址,修改为 ...
- js获取URL地址中的GET参数
var $_GET = (function(){ var url = window.document.location.href.toString(); var u = url.split(" ...
- svn cleanup failed问题解决
1.SVN出错 今早过来Update,报如下错误: 再次更新,svn会要求你执行clean up,但执行clean up仍会报错,说有未完的work item,还要求你执行clean up.汗,陷入死 ...