[LeetCode]—Word LadderII 单词递推II
来源:互联网 发布:概率图模型 知乎 编辑:程序博客网 时间:2024/06/10 14:09
Word Ladder II
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"] ]在wordLadder 基础上,需要记录路径,记录前驱节点,最后使用深搜来查找路径。要保证路径最短,那么要注意同层之间不能互指,访问过的结点不能再访问。
学习自:https://gitcafe.com/soulmachine/LeetCode
class Solution {public: vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { unordered_set<string> visited; //判重 unordered_set<string> current,next; //当前层和下一层,用集合是为了去重 unordered_map<string,vector<string> > father; int level=0; //层数 bool found=false; current.insert(start); while(!current.empty() && !found){ ++level; //将本层全部置为已访问,防止同层之间互指,导致路径非最短 for(auto word : current) visited.insert(word); for(auto word : current){ for(size_t i=0;i<word.size();++i){ string new_word=word; for(char c='a';c<='z';c++){ if(c==new_word[i])continue; swap(c,new_word[i]); if(new_word==end)found=true; //找到了 if(visited.count(new_word)==0 && (dict.count(new_word)>0 || new_word==end)){ next.insert(new_word); father[new_word].push_back(word); } swap(c,new_word[i]); } } } current.clear(); swap(current,next); } vector<vector<string> > result; if(found){ vector<string> path; buildPath(father,path,start,end,result); } return result; }private: void buildPath(unordered_map<string,vector<string> > &father, vector<string> &path,const string &start,const string &word, vector<vector<string> > &result){ path.push_back(word); if(start==word){ result.push_back(path); reverse(result.back().begin(),result.back().end()); }else{ for (auto f : father[word]) buildPath(father,path,start,f,result); } path.pop_back(); }};
0 0
- [LeetCode]—Word LadderII 单词递推II
- [LeetCode]—Word Ladder 单词递推
- [leetcode]126. Word LadderII@Java解题报告
- Leetcode 212 Word Search II 单词查找
- LeetCode | Word Ladder II(单词梯II)
- LeetCode 291. Word Pattern II(单词模式II)
- LeetCode OJ 之 Word Search II (单词搜索 - 二)
- LeetCode 212. Word Search II(单词搜索)
- LeetCode 126. Word Ladder II(单词梯子)
- LeetCode 140. Word Break II(单词切分)
- LeetCode 264 Ugly Number II (递推)
- LeetCode——Word Ladder II
- LeetCode——Word Break II
- leetcode 126 —— Word Ladder II
- LeetCode — Word Break II 解题报告
- LeetCode—212 Word Search II
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- Akka入门安装以及示例(Java)
- java1.5高新知识之泛型(2)
- myeclipse10创建maven springmvc项目3
- 成佩涛-mysql字符串函数
- iOS 多线程 锁 互斥 同步
- [LeetCode]—Word LadderII 单词递推II
- android 9PNG图片制作
- SAP CRM Transaction Codes List
- Java读写Properties文件
- [Linux内存]linux内存分配函数总结
- activiti pom依赖
- 迷宫的最短路径(BFS的简单应用)
- 新闻代码
- [网络子系统] Netfilter机制分析