【题解】【字符串】【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 ...
随机推荐
- Linux下配置用msmtp和mutt发邮件
Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...
- mysql在一台服务器搭建主从1
1. 登录mysq的方法: mysql-S /tmp/mysql.sock 登录3306 mysql -S /tmp/mysql_slave.sock 登录3307 mysql -h 127.0. ...
- 好用的json-path
$.store.book[?(@.price < 10)].title Here is a complete overview and a side by side comparison of ...
- MySql 分组排序取时间最大的一条记录
SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY ...
- hdu 4602 Partition
http://acm.hdu.edu.cn/showproblem.php?pid=4602 输入 n 和 k 首先 f(n)中k的个数 等于 f(n-1) 中 k-1的个数 最终等于 f(n-k+1 ...
- PowerMock使用遇到的一些问题
首先使用PowerMock Mock对象如果不成功的话首先要检查在测试类上是否有这两个声明@RunWith(PowerMockRunner.class) ...
- C++string的操作
#include <iostream> using namespace std; int main() { //initilization string str("abc.ddd ...
- WebSockets基础
网络套接字是下一代WEB应用程序双向通信技术,它是基于一个独立的socket并且需要客户端浏览器支持HTML5. 一旦你了解了网络套接字与WEB服务器的连接,你将可以从浏览器发送数据到服务器并且可以接 ...
- Hibernate4 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- 黑客界大拿tombkeeper文章:怎么学好技术成为技术大拿(题目我自拟的)
这两天论坛上又有人开始抱怨世风日下,大家都现实了,都不开放了,不交流了.对这种“月经贴”,我基本上已经习惯了,不过因为吃了粉皮炖鸡,心情比较好,于是就说了两句. 三四年前,当时我对人性的看法还不像现在 ...