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 ...
随机推荐
- C/C++的二分查找
假设有一种温度传感器,已经测得它的电压和温度的对应关系,将电压值以ADC转换后的数字量的值表示,形成温度-AD值的对照表,如下. 大致成一条反比关系的曲线. ADC的底层驱动已经写好,对外有一个接口可 ...
- Centos7安装MySQL8.0(RPM方式)
人生处处皆学问,工作也是如此!过去不止一次在Linux上安装MySQL,可以说轻车熟路,但是写篇文章总结一下,发现有很多细节值得学习! 安装包选择 为什么用rpm? 在Linux系列上安装软件一般有源 ...
- Oracle 数据库下赋予用户的执行存储过程和创建表权限
grant create any table to username; grant create any procedure to username; grant execute any proced ...
- Avoid mutating a prop directly since the value will be overwritten whenever the parent component re
子组件修改父组件的值踩坑 Vue1.0升级至2.0之后,直接在子组件修改父组件的值是会报错的 目的是为了阻止子组件影响父组件的数据. 我们都知道在vue中,父组件传入子组件的变量是存放在props属性 ...
- java的各种集合为什么不安全(List、Set、Map)以及代替方案
我们已经知道多线程下会有各种不安全的问题,都知道并发的基本解决方案,这里对出现错误的情况进行一个实际模拟,以此能够联想到具体的生产环境中. 一.List 的不安全 1.1 问题 看一段代码: publ ...
- Python数据类型---数值类型
一.整数(Integer)简称Int,又称整型,由正整数.负整数.0构成,不包括小数,分数. a,b=1,2 #一种赋值方法,此时a=1,b=2 print(a+b) #加法 3 print(a-b) ...
- 灵魂拷问:你真的理解System.out.println()执行原理吗?
原创/朱季谦 灵魂拷问,这位独秀同学,你会这道题吗? 请说说,"System.out.println()"原理...... 这应该是刚开始学习Java时用到最多一段代码,迄今为止 ...
- php超全局数组 为什么swoole的http服务不能用
php的超全局数组$_GET等九个 可以直接使用 无需定义 实际上是浏览器请求到Apache或者nginx的时候 转发到PHP处理模块 fpm转发给php解释器处理 php封装好后丢给php的 sw ...
- scrapy 管道里面使用mysql插入数据库 python操作mysql
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to t ...
- django环境安装与项目创建方式
1.安装django pip install django2.检查django版本 : python -m django --version 3.创建项目 django-admin startproj ...