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 ...
随机推荐
- unsigned int 和 int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...
- P4568 [JLOI2011]飞行路线 / P2939 [USACO09FEB]Revamping Trails G
题目描述 Link Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在 \(n\) 个城市设有业务,设这些城市分别标记为 \(0\) 到 \(n-1\),一共 ...
- 从零开始针对 .NET 应用的 DevOps 运营实践 - 运行环境搭建
一.Overview 最近的一段时间,在公司里我都在进行基于 Jenkins 和 SonarQube 配合已有的 Gitlab 搭建部门的持续集成环境的工作,虽然之前有使用过 GitHub Actio ...
- 在Windows7系统中设置虚拟内存大小
当我们的电脑物理内存空间不够用时,操作系统就会自动从硬盘空间上分出一块空间来当内存使用,这就是虚拟内存.可以说虚拟内存是物理内存的补充,是备用的物理内存.一般来说,如果电脑里的程序不多,占用内存资源不 ...
- .NET Standard 类库的使用技巧
系列目录 [已更新最新开发文章,点击查看详细] 在前一篇博客<.NET Standard中配置TargetFrameworks输出多版本类库>中详细介绍了如何创建.配置.条件编译. ...
- 多测师讲解接口测试 _理论基础知识001_高级讲师肖sir
前言: 我们今天进入接口测试的学习! 今天学习的内容是偏向理论 接口理论 了解接口测试(1) 一.什么是接口测试? 接口统称api,即程序与程序之间的对接.交接.交互.是测试系统组件间接口的一种测试. ...
- C# 范型约束 new() 你必须要知道的事
C# 范型约束 new() 你必须要知道的事 注意:本文不会讲范型如何使用,关于范型的概念和范型约束的使用请移步谷歌. 本文要讲的是关于范型约束无参构造函数 new 的一些底层细节和注意事项.写这篇文 ...
- MeteoInfoLab脚本示例:TOMS HDF数据
TOMS (Total Ozone Mapping Spectrometer)数据是全球臭氧观测.脚本程序: #Add data file folder = 'D:/Temp/hdf/' fns = ...
- Gradle的构建过程都不会?带你全面了解Android如何自定义Gradle 插件
目前 Android 工程的默认构建工具为 Gradle,我们在构建 APK 的时候往往会执行 ./gradlew assembleDebug 这样的命令.. 那么这个命令到底代表着什么含义呢?命令的 ...
- day63 Pyhton 框架Django 06
内容回顾 1.装饰器 装饰器:是一个闭包函数,在不改变原函数的代码和调用方式的基础上,给原函数增加功能. def wrapper(func): def inner(*args,**kwargs): # ...