https://oj.leetcode.com/problems/word-ladder-ii/

啊,终于过了

class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > ans; if(start.size() == || end.size() == || dict.size() == )
return ans; if(start == end)
{
vector<string> piece;
piece.push_back(start);
ans.push_back(piece);
return ans;
} unordered_map<string, vector<string> > parents; unordered_set<string> current;
current.insert(start);
dict.erase(start); unordered_set<string> next;
bool flagFind = false;
vector<string> lastOnes; // 记录最后一个变换的string
int depth = ; // 记录深度 while(current.size() != && flagFind == false) // flagFind 标记是否已经找到
{
depth++;
// 对于本层的每一个单词
unordered_set<string>::iterator itr;
for(itr = current.begin(); itr != current.end(); itr++)
{
// 对于本单词的每一个位置
for(int index = ; index < start.size(); index++)
{
// 替换成 a~z,并且不等于原单词,并且在dict中存在
for(char ch = 'a'; ch <= 'z'; ch++)
{
string newStr = *itr;
if(newStr[index] != ch) // 换了以后不是原来的那个
{
newStr[index] = ch;
if(newStr == end)
{
lastOnes.push_back( *itr);
flagFind = true;
}
}
else
continue; // 如果变换后在 dict 里面
if(dict.find(newStr) != dict.end())
{
next.insert(newStr);
// record parents
parents[newStr].push_back( *itr);
}
}
}
}
// remove all next strings from dict
for(itr = next.begin(); itr != next.end(); itr++)
dict.erase(*itr); current = next;
next.clear();
}
if(flagFind == true)
{
vector<string> ansPiece;
ansPiece.push_back(end);
buildPath(ans,lastOnes,ansPiece,parents,depth);
}
return ans;
}
void buildPath(vector<vector<string> > &ans,vector<string> &lastOnes, vector<string> &ansPiece, unordered_map<string,vector<string> > &parents,int depth)
{
depth--;
for(int i = ; i < lastOnes.size(); i++)
{
ansPiece.push_back(lastOnes[i]);
if(depth == )
{
vector<string> temp = ansPiece;
reverse(temp.begin(),temp.end());
ans.push_back(temp);
}
else
{
buildPath(ans,parents[lastOnes[i]],ansPiece,parents,depth);
}
ansPiece.pop_back();
}
}
};

LeetCode OJ-- Word Ladder II ***@的更多相关文章

  1. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  2. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  3. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  4. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  5. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  6. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  7. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

  9. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...

  10. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

随机推荐

  1. Hardmard 变换

    阿达马(Hadamard)矩阵是由+1和-1元素构成的正交方阵.阿达马变换多被用来计算SATD(一种视频残差信号大小的衡量). 这里介绍三个内容,1. SATD 2. H264中阿达马的应用 3. 阿 ...

  2. 双击vbs时,默认cscript运行脚本

    Dim obj_shellset obj_shell = createobject("wscript.shell")host = WScript.FullNameIf LCase( ...

  3. Mysql函数instr、locate、position VS like

    Mysql 大家都会用这样的語法: SELECT `column` FROM `table` where `condition` like `%keyword%’ 事实上,可以使用 locate 和 ...

  4. ESL python调用C模块时传递unicode字符串报错问题解决

    在是用freeswitch时利用ESL的python调用时传递字符串报错 TypeError: in method 'ESLconnection_api', argument 2 of type 'c ...

  5. CSS-页面布局

    介绍 几个实现多栏布局的方法.主要介绍使用内部div来创建浮动的栏. 多栏布局有三种基本的实现方案:固定宽度.流动.弹性. 固定宽度布局的大小是随用户调整浏览器窗口大小而变化,一般是900至1100像 ...

  6. 去掉DLL can move

    1.OptionalHeader.DllCharacteristics = wNewDllCharacteristics; 用CFF打开,如果存在DLL can move这个选项,去掉即可 2.Rel ...

  7. 5.Mybatis的输出映射(就是对查询的结果集的映射)

    Mybatis的输出映射,也就是对查询结果集的一个映射,主要有两种: 1.resultType(不需要配置,可以直接用) 一般是实体类 基本类型也可以 2.resultMap(需要配置resultMa ...

  8. jquery移动端日期插件

    不说多的,直接看代码<!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. SSH基本框架搭建后的简化

    对于SSh框架的简化,我们可以从下面几个方面来剖析: 1.实体类entity:在这里我们需要将数据库和实体类进行关联,在简化之前,我们需要在entity包里面加入一份.xml配置文件 例如原码---- ...

  10. Linux socket 获得本地IP和广播地址

    #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netine ...