LeetCode 127. Word Ladder II
来源:互联网 发布:网络订餐平台 编辑:程序博客网 时间:2024/06/05 20:56
LeetCode 126. Word Ladder的进阶,需要用到126里的处理办法:
1. dict.insert(end), 将end加入到dict中,这样不用到最后再特判
2. 对于每个字符串,每次只变换它个一个字母,看这个新串是否在dict中(详情见126题解)
同时感觉思路不像网上一些博主说的那么复杂...
用dis(map<string, int>类型)记录每个单词离start的距离,通过BFS记录每个单词的前继单词paths(map<string, vector<string>>);
再通过paths信息从end递归(可以理解为DFS), 求出到end的道路即可。
不过这题是比较难...
代码:
class Solution{public:vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {queue<string> q;q.push(start);dis[start] = 1;dict.insert(end);while (q.empty() == false){string cur = q.front();for (size_t i = 0; i < cur.size(); ++ i){for (int j = 0; j < 26; ++ j){string tmp = cur;tmp[i] = 'a' + j;if (dict.find(tmp) != dict.end()){if (dis.find(tmp) == dis.end()){dis[tmp] = dis[cur] + 1;paths[tmp].push_back(cur);q.push(tmp);} else if (dis[cur] + 1 == dis[tmp]){paths[tmp].push_back(cur);}}}}dict.erase(cur);q.pop();}return recurse(end);}private:vector<vector<string>> recurse(const string& cur){vector<vector<string>> ret;for (auto it = paths[cur].begin(); it != paths[cur].end(); ++ it){vector<vector<string>> tmp(recurse(*it));for (auto itt = tmp.begin(); itt != tmp.end(); ++ itt){itt->push_back( cur );ret.push_back(*itt);}}if (dis[cur] == 1){ret.push_back( vector<string>(1, cur) ); // cur==begin}return ret;}map<string, int> dis;map<string, vector<string>> paths;};
0 0
- LeetCode 127. Word Ladder II
- Leetcode 127. Word Ladder I & 126. Word Ladder II
- 126. Word Ladder II 、 127. Word Ladder(leetcode BFS+DFS)
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- [LeetCode] Word Ladder II
- Word Ladder II -- LeetCode
- Leetcode: Word Ladder II
- leetcode-Word Ladder II
- leetcode-Word Ladder II
- Android Push Notification实现信息推送使用
- uvalive3882(约瑟夫环)
- Android基础教程(二)之五大布局对象---FrameLayout,LinearLayout ,AbsoluteLayout,RelativeLayout,TableLayout.
- Linux下通用的目录架构Makefile
- git常见问题解决方法
- LeetCode 127. Word Ladder II
- 时间序列异常事件检测
- 用jquery向服务器发送请求get&post
- error MSB6006:"mt.exe" exited with code 31的解决办法
- 常见排序算法
- Eclipse离线加载并更新Android SDK
- eclipse 实用快捷键
- 【存储专题】西瓜哥带您学习高端存储
- Android开发中使用adb shell命令出现sqlite3 : not found 问题