leetcode24:word-ladder-ii
题目描述
[↵ ["hit","hot","dot","dog","cog"],↵ ["hit","hot","lot","log","cog"]↵ ]
注意:
- 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"]↵ ]↵
class Solution {public:/* vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string>> paths; vector<string> path(1, start); if(start == end){ paths.push_back(path); return paths; } unordered_set<string> forward, backward; forward.insert(start); backward.insert(end); unordered_map<string, vector<string>> nexts; bool isForward = false; if(findLaddersHelper(forward, backward, dict, nexts, isForward)) getPath(start, end, nexts, path, paths); return paths; }private: bool findLaddersHelper(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, unordered_map<string, vector<string>> nexts, bool &isForward){ if(forward.empty()) return false; if(forward.size() > backward.size()) return findLaddersHelper(backward, forward, dict, nexts, isForward); //从words数较少的一边开始寻路 for(auto it=forward.begin(); it!=forward.end(); it++) dict.erase(*it); for(auto it=backward.begin(); it!=backward.end(); it++) dict.erase(*it); unordered_set<string> nextLevel; bool reach = false; for(auto it=forward.begin(); it!=forward.end(); ++it){ string word = *it; for(auto ch=word.begin(); ch!=word.end(); ++ch){ char tmp = *ch; for(*ch='a'; *ch<='z'; ++(*ch)){ if(*ch != tmp) //遍历除自身外的25个字母 if(backward.find(word) != backward.end()){ reach = true; //走到了末尾 isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it); } else if(!reach && dict.find(word) != dict.end()){ nextLevel.insert(word); isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it); } } *ch = tmp; } } return reach || findLaddersHelper(backward, nextLevel, dict, nexts, isForward); } void getPath(string beginWord, string &endWord, unordered_map<string, vector<string>> &nexts, vector<string> &path, vector<vector<string>> &paths){ if(beginWord == endWord) paths.push_back(path); else for(auto it=nexts[beginWord].begin(); it!=nexts[beginWord].end(); ++it){ path.push_back(*it); getPath(*it, endWord, nexts, path, paths); path.pop_back(); } }*/ vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> > paths; vector<string> path(1, start); if (start == end) {//首位words相同 paths.push_back(path); return paths; } unordered_set<string> forward, backward; forward.insert(start); backward.insert(end); unordered_map<string, vector<string> > nexts; //存储路径的矩阵 bool isForward = false; if (findLaddersHelper(forward, backward, dict, nexts, isForward)) getPath(start, end, nexts, path, paths); return paths; }private: bool findLaddersHelper( unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, unordered_map<string, vector<string> > &nexts, bool &isForward) { isForward = !isForward; //反转方向标志?? if (forward.empty()) return false; if (forward.size() > backward.size()) return findLaddersHelper(backward, forward, dict, nexts, isForward);//从words数较少的一边开始寻路 for (auto it = forward.begin(); it != forward.end(); ++it) //已放入前向 后向数组中的words从dict去除 dict.erase(*it); for (auto it = backward.begin(); it != backward.end(); ++it) dict.erase(*it); unordered_set<string> nextLevel; bool reach = false; //寻路未完成 for (auto it = forward.begin(); it != forward.end(); ++it) {//广度遍历前向数组中的每一个分支 string word = *it; for (auto ch = word.begin(); ch != word.end(); ++ch) { char tmp = *ch; for (*ch = 'a'; *ch <= 'z'; ++(*ch))//遍历除自身外的25个字母 if (*ch != tmp) if (backward.find(word) != backward.end()) { //前后向数组成功相接 reach = true; //寻路完成 isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it); } else if (!reach && dict.find(word) != dict.end()) { //未到达 且 字典中有需要的words nextLevel.insert(word); //将新产生的分支放入临时数组,用于下次递归调用 isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it); } *ch = tmp; } } return reach || findLaddersHelper(backward, nextLevel, dict, nexts, isForward); } void getPath( string beginWord, string &endWord, unordered_map<string, vector<string> > &nexts, vector<string> &path, vector<vector<string> > &paths) { if (beginWord == endWord) //走到了,将path中的值压入paths paths.push_back(path); else for (auto it = nexts[beginWord].begin(); it != nexts[beginWord].end(); ++it) { path.push_back(*it); getPath(*it, endWord, nexts, path, paths); path.pop_back(); //每退出一次递归,将该层压入的值弹出 } }};leetcode24:word-ladder-ii的更多相关文章
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- CSS中居中的完全指南(中英对照翻译)
翻译自:https://css-tricks.com/centering-css-complete-guide/ Centering things in CSS is the poster child ...
- 一文看懂YOLO v3
论文地址:https://pjreddie.com/media/files/papers/YOLOv3.pdf论文:YOLOv3: An Incremental Improvement YOLO系列的 ...
- C# 主界面的扁平化
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...
- CSS精灵图与字体图标
CSS精灵图与字体图标 1. 精灵图 当用户访问一个网站时,需要向服务器发送请求,网页上的每张图像都要经过一次请求才能展现给用户.然而,一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多 ...
- 多测师讲解selenium _enter弹框_高级讲师肖sir
enter # from selenium import webdriver# from time import sleep# drvier=webdriver.Chrome()# url='file ...
- day45 Pyhton 数据库Mysql 02
一.前期回顾 数据库 mysql的安装 配置环境 为什么要用数据库? 稳定性 一致性 并发 存取数据效率高 数据库的分类 关系型数据库 mysql oracle sqlserver 非关系型数据库 r ...
- C++11随机数库
random随机数库 C++11引入了新的随机数生成机制,那就是<random>随机数库,支持多种伪随机数生成算法,多种连续和离散随机数分布算法,以及封装了真正的随机数生成引擎random ...
- 解了这14道C语言谜题后,所有人都失声了!我来带你深入了解C!
本文展示了14个C语言的迷题以及答案,代码应该是足够清楚的,而且有相当的一些例子可能是我们日常工作可能会见得到的.通过这些迷题,希望你能更了解C语言. 如果你不看答案,不知道是否有把握回答各个谜题?让 ...
- PHP SPL标准库-接口
PHP SPL标准库有一下接口: Countable OuterIterator RecursiveIterator SeekableIterator SplObserver SplSubject A ...
- 第二章 OSI参考模型
一.产生背景 1.伴随着计算机网络的飞跃发展,各大厂商根据自己的协议生产出了不同的硬件和软件 2.为了实现网络设备间的互相通讯,ISO和IEEE相继提出了OSI参考模型及其TCP/IP模型 二.OSI ...