LeetCode OJ-- Word Ladder II ***@
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 ***@的更多相关文章
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode#128]Word Ladder II
Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...
- LeetCode OJ——Word Ladder
http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
随机推荐
- NGUI之UILabel
重要属性说明 overflow: Shrink Content: 意味着内容会自动被缩小以便适应区域.它在你使用动态字体的同时使用Keep Crisp设置时有效,让字体变小,而不是缩放内容. ...
- The Magic only works with total devotion of one's heart
The Magic only works with total devotion of one's heart All tools and equipments are useless without ...
- 转:RTC搭建android下三层应用程序访问服务器MsSql-客户端
原文:http://www.cnblogs.com/delphi007/p/3346084.html android下stringgrid已知问题: 通过点击时获取对应行的值有问题,在win下调试正常 ...
- 内省(Introspector)
内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...
- java中Thread的 interrupt异常处理
http://blog.csdn.net/srzhz/article/details/6804756
- Java和C++中的static
1.Java类中的static变量和static方法会在类装载的过程中就得到内存分配,然后就会进行初始化工作.最多可能会被初始化3次,静态代码块的执行在main方法之前. static变量不可以在构造 ...
- 三级联动---DropDownList控件
AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...
- JavaScript中的Map
1.首先,在新版本的浏览器中,已经实现了对Map的原生支持:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob ...
- mysql分区查询
SELECT *FROM INFORMATION_SCHEMA.partitions WHERE table_name='表名' and table_schema='数据库名'
- Linux驱动框架之framebuffer驱动框架
1.什么是framebuffer? (1)framebuffer帧缓冲(一屏幕数据)(简称fb)是linux内核中虚拟出的一个设备,framebuffer向应用层提供一个统一标准接口的显示设备.帧缓冲 ...