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. 转来的。。。 关于return 的一些事情

    转来的 http://blog.csdn.net/haiwil/article/details/6691854/ 一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局 ...

  2. eclipse默认文件编码

    eclipse里 就是在eclipse.ini文件里添加一行-Dfile.encoding=utf-8即可 -startup plugins/org.eclipse.equinox.launcher_ ...

  3. Zero Requiem

    “最后是在游行.暴君鲁路修高居王座,两侧列着所有反对者的代表:黑色骑士团.黎星刻.原圆桌骑士名列第三的吉诺,以及一身女囚装的娜娜丽,他们都即将被公开处死.尤菲米娅在第一次“特别行政区•日本”成立仪式上 ...

  4. LeetCode 342. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  5. 神奇的C语言

    当然下面列出来的几点都是C的基础用法,只不过是这些用法可能平时不会被注意.所以很多东西第一次看到的时候,可能会觉得很怪异,但是细细想想就能很好的理解,也就能更好的清楚C语言的一些特性.但是在具体的编码 ...

  6. sql(转自http://www.imooc.com/article/2325)

    http://www.imooc.com/article/2325

  7. win32手动创建windows窗口的,小记

    摘抄自文档,其中的函数需要以后花时间看 向 WinMain 添加功能 首先,在 WinMain 函数内部创建 WNDCLASSEX 类型的窗口类结构. 此结构包含有关窗口的信息,如应用程序图标.窗口的 ...

  8. Mysql忘记用户密码的解决办法

    1.1 忘记用户密码的解决办法 普通用户,直接用root超级管理员登录进去修改密码就可以了,但是如果root密码丢失了,怎么办呢? 1.1.1 msyqld_saft方式找回密码 停止mysql:se ...

  9. TIJ读书笔记08-数组的初始化和可变长参数形参

    TIJ读书笔记08-数组的初始化和可变参数形参 数组 数组的声明 数组的初始化和赋值 可变参数列表 数组 相同类型的,用一个标识符名称封装到一起的一个对象序列或者基本数据类型序列叫数组.(多么严谨的概 ...

  10. angularjs 延迟更新和angularjsUI

    angularjsUI库https://material.angularjs.org/latest/ ng-model-options="{ updateOn: 'blur' }" ...