Word Ladder II
来源:互联网 发布:两组数据差值的标准差 编辑:程序博客网 时间:2024/06/08 04:29
class Solution {public: vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> >ans; if(start == end) return ans; unordered_set<string>current , next; unordered_set<string> flag; unordered_map<string,vector<string> > father; current.insert(start); bool found = false; while(!current.empty() && !found) { //expand for(const auto &x : current) { flag.insert(x); } for(auto x : current) { for(int i = 0 ; i < x.size() ; ++i) { for(int j = 'a' ; j <= 'z' ; ++j) { if(x[i] == j) continue; string tmp = x; tmp[i] = j; if(tmp == end) found = true; if(dict.find(tmp) != dict.end() && flag.find(tmp) == flag.end()) { next.insert(tmp); father[tmp].push_back(x); } } } } //end expand current.clear(); swap(current, next); } //start foudn father if(found) { vector<string> c; dfs(ans , father , c , start , end); } return ans; }private: void dfs(vector<vector<string> >&ans, unordered_map<string,vector<string> >& father , vector<string>& c , string& start , string& now) { c.push_back(now); if(now == start) { ans.push_back(c); reverse(ans.back().begin() , ans.back().end()); c.pop_back(); return; } auto que = father.find(now) -> second; for(auto& x : que) { dfs(ans , father , c , start , x); } c.pop_back(); }};
0 0
- Word Ladder/ Word Ladder II
- Word Ladder II
- Word Ladder II
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- Word Ladder II
- Word Ladder II
- Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- PHP参数修改不生效的问题
- 如何判断$_POST传送的<textarea>标签中的值是否为空
- Reorder List
- Sudoku Solver
- Orace新建临时表空间、数据表空间、用户
- Word Ladder II
- Android Activity和Fragment的转场动画
- 表结构文档(考核准备)
- Add Binary
- Valid Sudoku
- 玻璃钢废气净化塔的安装与维护保养
- 16位灰度图之TIFF
- codeigniter下的表单辅助函数form_open
- oracle 建立索引常用的规则