本题有几个注意点:

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. php异常处理的深入

    引出 如果你调一个类,调用时数据验证时报了个错,你会以什么方式返回 数组,布尔值? 数组这个可以带错误原因回来,那布尔值呢? 返回了个 false, 报错时把错误放在类变量里?还是专门用一个获取错误的 ...

  2. docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置

    原文:docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_3767485 ...

  3. Maven的SSH搭建以及部署

    本人有点傻,研究Maven研究了有一段时间,刚刚有些入门,记录下来方便以后使用 工作环境:jdk7 myeclipse10 maven3.1.1 1 下载maven3.1.1 http://maven ...

  4. HDU3265 Examining the Rooms【stirling数】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=3625 题目大意: 有N个房间,每一个房间的要是随机放在某个房间内,概率同样.有K次炸门的机会. 求 ...

  5. JAVA并发-为现有的线程安全类添加原子方法

    JAVA中有许多线程安全的基础模块类,一般情况下,这些基础模块类能满足我们需要的所有操作,但更多时候,他们并不能满足我们所有的需要.此时,我们需要想办法在不破坏已有的线程安全类的基础上添加一个新的原子 ...

  6. php中局部变量和全局变量

    php中局部变量和全局变量 代码1:函数内部使用函数外部变量错误方法 <?php $name = 'fish'; function animal() { echo $name; } animal ...

  7. SpringBoot结合MongoDB入门

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  8. 79.express里面的app.configure作用

    以下摘自 express 3.0 的 文档 app.configure([env], callback) Conditionally invoke callback when env matches ...

  9. 计算文件大小(C/C++语言)

    #include <stdio.h> int main() { FILE* fp; if (fp = fopen("read files.exe", "r&q ...

  10. 关于commJS 和 es6 的一些区别

    CommonJS模块与ES6模块的区别 本文转自 https://www.cnblogs.com/unclekeith/archive/2017/10/17/7679503.html CommonJS ...