本题有几个注意点:

1. 回溯找路径时。依据路径的最大长度控制回溯深度

2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束。不须要遍历下一层了。

3. 能够将字典中的单词删除。替代visited的set,这样优化以后时间从1700ms+降到800ms+

代码例如以下:

class Solution {
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
set<string> queue[2];
queue[0].insert(start);
vector<vector<string>> res;
bool find = false;
int length = 1;
bool cur = false;
map<string, set<string>> mapping; //bfs
while (queue[cur].size() && !find)
{
length++;
for (set<string>::iterator i = queue[cur].begin(); i != queue[cur].end(); i++)//delete from dictionary
dict.erase(*i);
for (set<string>::iterator i = queue[cur].begin(); i != queue[cur].end(); i++)
{
for (int l = 0; l < (*i).size(); l++)
{
string word = *i;
for (char c = 'a'; c <= 'z'; c++)
{
word[l] = c;
if (dict.find(word) != dict.end())
{
if (mapping.find(word) == mapping.end()) mapping[word] = set<string>();
mapping[word].insert(*i);
if (word == end) find = true;
else queue[!cur].insert(word);
}
}
}
}
queue[cur].clear();
cur = !cur;
}
if (find)
{
vector<string> temp;
temp.push_back(end);
getRes(mapping, res, temp, start, length);
} return res;
} void getRes(map<string, set<string>> & mapping, vector<vector<string>> & res, vector<string> temp, string start, int length)
{
if (temp[0] == start)
{
res.push_back(temp);
return;
}
if (length == 1) return;//recursion depth
string word = temp[0];
temp.insert(temp.begin(), "");
for (set<string>::iterator j = mapping[word].begin(); j != mapping[word].end(); j++)
{
temp[0] = *j;
getRes(mapping, res, temp, start, length - 1);
}
}
};

Word Ladder II [leetcode]的更多相关文章

  1. Word Ladder II leetcode java

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

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. [Leetcode Week5]Word Ladder II

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

  4. 【leetcode】Word Ladder II

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

  5. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  6. LeetCode: Word Ladder II 解题报告

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

  7. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  8. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  9. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

随机推荐

  1. Spring 容器(一)

    Source null instanceof Object 语法是正确的 Source定义得到输入流的方法 Resource继承Source,定义判断流是否存在.是否打开等方法 BaseResourc ...

  2. sp_executesql invalid object name

    https://stackoverflow.com/questions/10417126/dynamically-named-temp-table-returns-invalid-object-nam ...

  3. 22. Angular 中用 a 标签 href 路由时在浏览器中显示异常 "%2F" 路由失败问题

    转自:https://blog.csdn.net/duansale/article/details/77455355 <a href="#/index">index&l ...

  4. vue父子间通信案列三($emit和prop用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. canvas中遇到的理解问题

    1.lineDashOffset ctx.lineDashOffset = number 描述: setLineDash 用于设置开始绘制虚线的偏移量. 数字的正负表示左右偏移. 2.createLi ...

  6. 分享《Python 游戏编程快速上手(第3版)》高清中文版PDF+高清英文版PDF+源代码

    通过编写一个个小巧.有趣的游戏来学习Python,通过实例来解释编程的原理的方式.14个游戏程序和示例,介绍了Python基础知识.数据类型.函数.流程控制.程序调试.流程图设计.字符串操作.列表和字 ...

  7. 研究一些复杂java开源软件代码的体会(转)

    原文地址:http://herman-liu76.iteye.com/blog/2349026     有时候看源代码是非常有趣的事情,象是思考游戏,象是思考棋局...     平时做J2EE项目中, ...

  8. java设计模式--事件监听器模式(观察者模式)

    这两个模式实质上很简单,在实际项目中也是非常常用的.但却被有些人说的云里雾里,这里用白话解释一下. 本质上两者都是同一个模式.专业的说法是这样的(觉得绕口的请直接转到白话解释部分,再回头来看下面这几句 ...

  9. HDU 1429 胜利大逃亡(续)(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. Resize图片

    在网站上传图片的时候,提示图片太大了. 有5种方式来调整图片大小 http://www.wikihow.com/Resize-a-JPEG picresize.com 这个网站比较靠谱:使用Windo ...