[leetcode] 126. Word Ladder II 解题报告
来源:互联网 发布:终端写c语言 编辑:程序博客网 时间:2024/06/05 09:37
题目链接: https://leetcode.com/problems/word-ladder-ii/
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord toendWord, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Return
[ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
Note:
- All words have the same length.
- All words contain only lowercase alphabetic characters.
大概过程差不多, 但是有点不同的是当我们将字典中的一个字符串删除的时候在另一条路径上可能还会用到这个字符. 也就是像这样:
A -> C -> D, B->C->D
他们都会经过C, 并且两个都是最短的路径, 在A的时候搜索到C, 并且将C从字典中删除, 当B在搜索与其距离为1的字符串时, C已经不在字典中了, 那么怎么办呢? 我们设置一个hash表用来存储一个字符串的父结点集合, 这样C不在字典中再去查hash表看C是否在hash表中, 如果在的话并且C的父结点层次和B一样, 那么就将B也加入到C的父结点结合中去. 可以知道, 一个字符串的父结点集合的距离起点的距离必然是相等的, 也就是说他们都是最短距离.
最后遍历完所有的点之后, 再用DFS从终点往前找出所有集合即可.
不得不吐槽一句这是哪个变态的公司问的问题, 第二遍写的时候居然还不如第一次写的, 可能我已经昏了, 毕竟已经快凌晨4点了, 本来可以三点睡的, 可恶!
代码如下:
class Solution {public: void DFS(set<pair<string, int>> st, vector<string> vec) { for(auto val: st) { vec.push_back(val.first); if(hash.count(val.first)==0) { reverse(vec.begin(), vec.end()); return result.push_back(vec); } DFS(hash[val.first], vec); vec.pop_back(); } } vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) { wordList.insert(endWord); wordList.erase(beginWord); que.push(make_pair(beginWord, 1)); while(!que.empty()) { auto val = que.front(); que.pop(); for(int i = 0; i < val.first.size(); i++) { string tem = val.first; for(int j =0; j < 26; j ++) { tem[i] = 'a' + j; if(wordList.count(tem)) { que.push(make_pair(tem, val.second+1)); wordList.erase(tem); hash[tem].insert(val); } else if(hash.count(tem)&&hash[tem].begin()->second==val.second) hash[tem].insert(val); } } } DFS(hash[endWord], vector<string>{endWord}); return result; } vector<vector<string>> result; unordered_map<string, set<pair<string, int>>> hash; queue<pair<string, int>> que;};
- [leetcode] 126. Word Ladder II 解题报告
- [Leetcode] 126. Word Ladder II 解题报告
- Leetcode Word Ladder II 解题报告
- leetcode 解题报告 Word Ladder II
- Leetcode 126. Word Ladder II 字符变换2 解题报告
- LeetCode Word Ladder解题报告
- [LeetCode]Word Ladder,解题报告
- LeetCode Word Ladder解题报告
- [leetcode] 127. Word Ladder 解题报告
- [Leetcode] 127. Word Ladder 解题报告
- [leetcode]127. Word Ladder@Java解题报告
- [Leetcode] 126. Word Ladder II
- [leetcode] 126.Word Ladder II
- leetcode 126. Word Ladder II
- Leetcode 126. Word Ladder II
- Leetcode 126. Word Ladder II
- [leetcode] 126. Word Ladder II
- [LeetCode] 126. Word Ladder II
- Hadoop MapReduce 大矩阵乘法
- 使用AVAudioPlayer实现音乐播放器
- java面试题 用1,2,2,3,4,5六个数字,组成一个6位数。 第三位数不能是2;3和5不能相邻。
- 【hihocoder】#1082 : 然而沼跃鱼早就看穿了一切
- JBOSS安全配置
- [leetcode] 126. Word Ladder II 解题报告
- Shell Scripting basic Interview questions
- 生成模型与判别模型
- mysql优化设计方案
- 第11周项目3(1)——警察和厨师
- OWL-QN算法
- Oracle 12c 简单的jdbc使用
- Android实现ViewPager+Fragment
- 遗传算法的个人理解