LeetCode OJ Word Ladder II
来源:互联网 发布:centos l2tp 编辑:程序博客网 时间:2024/05/10 09:23
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]
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.
自己改成了C++版本,具体的思路就是把每个string当做一个点,先BFS找到边,然后DFS找路径,感觉有点像某道什么素数路径的题。
class Solution {public:vector<vector<string> > ans; // answervector<string> backtraceList; // list for backtracing, maybe an answermap<string, vector<string> > edges; // treat every string as a pointstring st; // start stringstring ed; // end stringunordered_set<string> visit; // BFS visitunordered_set<string> unvisit; // BFS unvisitvector<vector<string>> findLadders(string start, string end, unordered_set<string> & dict) {ans.clear();backtraceList.clear();edges.clear();if (dict.size() == 0) return ans;st = start;ed = end;visit.clear();unvisit = dict;BFS(); // bfs makes graphDFS(ed); // dfs find roads(from end to start)return ans;}void BFS() {int cur = 1; // current points to checkint next = 0; // next points to checkint l; // the length of each wordbool found = false;queue<string> q;q.push(st);unvisit.insert(ed);unvisit.erase(st); // maybe there is another start string in the dictwhile (!q.empty()) {string w = q.front();q.pop();cur--;l = w.size();for (int i = 0; i < l; i++) {char originalChar = w[i];string nw = w;for (char c = 'a'; c <= 'z'; c++) {nw[i] = c;if (unvisit.find(nw) != unvisit.end()) { // an unvisited wordif (visit.find(nw) == visit.end()) { // there is no this word in visitvisit.insert(nw);next++; // more word to visit next timeq.push(nw);}map<string, vector<string> >::iterator iter = edges.find(nw);if (iter != edges.end()) { // a new point of an existed edge(*iter).second.push_back(w);}else { // a new edgevector<string> temp;temp.push_back(w);edges[nw] = temp;}if (nw == ed) found = true;}}w[i] = originalChar;}if (cur == 0) {if (found) break;cur = next;next = 0;unordered_set<string>::iterator iter = visit.begin();for (; iter != visit.end(); iter++) unvisit.erase(*iter);visit.clear();}}}void DFS(string w) {if (w == st) { // find start stringvector<string> temp;temp.push_back(st);for (int i = 0, s = backtraceList.size(); i < s; i++)temp.push_back(backtraceList[s - i - 1]);ans.push_back(temp);return;}backtraceList.push_back(w);map<string, vector<string> >::iterator iter = edges.find(w);if (iter != edges.end()) {int s = iter->second.size();for (int i = 0; i < s; i++) DFS(iter->second[i]);}backtraceList.pop_back();}};
0 0
- LeetCode OJ - Word Ladder II
- LeetCode OJ Word Ladder II
- LeetCode OJ:Word Ladder
- LeetCode OJ - Word Ladder
- LeetCode OJ - Word Ladder
- LeetCode OJ Word Ladder
- 【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
- 第三周项目5-4:数组作成员数据-数据保存
- QML 自定义进度条样式
- Servlet 3.0 新特性详解(二)异步处理支持
- UVA 12301 - An Angular Puzzle(计算几何)
- hdu 2647 Reward 拓扑排序。
- LeetCode OJ Word Ladder II
- php 面试题收集
- android获取多媒体库的视频、音频、图片
- CCTV10专题《重访---黑客江湖》
- Subsets
- set::count
- Servlet 3.0 新特性详解(三)注解支持
- 分析.lds文件格式
- Win7+OpenCV2.4.10+VS2010安装教程