leetcode 126: Word Ladder ll (uncompleted)
来源:互联网 发布:电信卡怎么设置4g网络 编辑:程序博客网 时间:2024/06/08 14:55
Word Ladder IIFeb 11
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.
class Solution {public: vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<string> > res; if(start.size()!=end.size()) return res; // if(start == end) return ; unordered_set<string> unique; unique.insert(start); queue<string> que; unordered_map<string, string> path; que.push(start); path[start] = ""; int q1=1; int q2=0; while(q1>0) { string s = que.front(); que.pop(); --q1; string pre = ""; for(int i=0; i<s.size(); i++) { string temp = s; for(char c='a'; c<='z'; c++) { temp[i] = c; if(dict.find(temp)!=dict.end() && unique.find(temp)==dict.end()) { if(temp == end) { if(pre!="") { unique.erase(pre); } pre = temp; vector<string> elems; elems.push_back(end); string item = s; while(item!="") { elems.insert(elems.begin(), item); item = path[item]; } res.push_back(elems); } else { unique.insert(temp); que.push(temp); path[temp] = s; ++q2; } } } } if(q1==0) { if(!res.empty()) return res; q1 = q2; q2 = 0; } } return res; }};
class Solution {public: class TreeNode{ public: string val; TreeNode * parent; TreeNode(string str, TreeNode* p=NULL):val(str), parent(p){} }; bool findnode(string temp, TreeNode* p) { while(p!=NULL) { if( temp == p->val) return false; p = p->parent; } return true; } vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<TreeNode*> clean; vector<vector<string> > res; if(start.size()!=end.size()) return res; // if(start == end) return ; queue<TreeNode*> que; TreeNode *head = new TreeNode(start); clean.push_back(head); TreeNode *p = head; que.push(p); int q1=1; int q2=0; while(q1>0) { TreeNode * p = que.front(); string s = p->val; que.pop(); --q1; for(int i=0; i<s.size(); i++) { string temp = s; for(char c='a'; c<='z'; c++) { temp[i] = c; if(dict.find(temp)!=dict.end() && findnode(temp, p) ) { if(temp == end) { vector<string> elems; elems.push_back(end); while(p!=NULL) { elems.insert(elems.begin(), p->val); p = p->parent; } res.push_back(elems); } else { TreeNode* tn = new TreeNode(temp,p); clean.push_back(tn); que.push(tn); ++q2; } } } } if(q1==0) { if(!res.empty()) return res; q1 = q2; q2 = 0; } } for(int i=0; i<clean.size(); i++) { delete clean[i]; clean[i] = NULL; } return res; }};
- leetcode 126: Word Ladder ll (uncompleted)
- LeetCode: Word Ladder [126]
- [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
- [Leetcode][python]Word Ladder/Word Ladder II
- [leetcode] 126 Word Ladder ii bfs dfs
- leetcode 126 —— Word Ladder II
- [leetcode-126]Word Ladder II(java)
- [Leetcode] Word Ladder
- LeetCode: Word Ladder
- leetcode 84: Word Ladder
- [leetcode] word ladder
- 【leetcode】Word Ladder
- 转自别处的:J2EE架构师之路
- Windows启动过程分析-预引导
- The Single Responsibility Principle
- 每一个有理想的程序员都应该读的一本书-《rework》
- Start from Yes
- leetcode 126: Word Ladder ll (uncompleted)
- Step Back and Automate, Automate, Automate
- java HttpClient Post实例
- JNI学习(5)(我是菜鸟)
- Take Advantage of Code Analysis Tools
- 俞敏洪一分钟励志演讲:
- Test for Required Behavior, not Incidental Behavior
- Android的permission大全
- 在交互细节上,Android 与 iOS 有哪些区别?