本题有几个注意点:

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. 绿色版SecureCRT启动崩溃,遇到一个致命的错误且必须关闭

    百度搜了半天,大家都是说删除注册表的VanDyke就能解决问题,但是我用的是绿色版的,删除VanDyke后还不行. 然后试了一下重新解压出一个绿色版的SecureCRT,发现能用. 但之前我配置了很多 ...

  2. 怎样给UINavigationBar加入button?

    Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...

  3. Firefox中使用pac

    https://campus.barracuda.com/product/websecurityservice/article/WSS/ConfigProxyWithPACFile/ https:// ...

  4. wcf rest系列文章

    http://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html 需要注意的是,发布的服务,可以在web behavior中指定显示help ...

  5. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

  6. Linux下通过.desktop 文件创建桌面程序图标及文件编写方式(Desktop Entry文件概述)

    Linux下通过.desktop 文件创建桌面程序图标及文件编写方式 1.Desktop Entry文件概述:在 Windows 平台上,用户可以通过点击位于桌面或菜单上的快捷方式轻松打开目标应用程序 ...

  7. 关于props default 数组/对象的默认值应当由一个工厂函数返回

    export default {props: { xAxisData: {   type: Array,   default: [] }, },这是我的代码 报错是Invalid default va ...

  8. PHP获取一周后的时间戳

    echo strtotime("now");//相当于将英文单词now直接等于现在的日期和时间,并把这个日期时间转化为unix时间戳.这个效果跟echo time();一样. ec ...

  9. 一个Web报表项目的性能分析和优化实践(五):重构有助于性能优化么?

    项目从初次开发到现在,已经快3年了.期间,有N个工程师参与过. 需求方面:增加减少,反反复复,无数次:人力方面:增加减少,不稳定:时间方面:功能开发着急上线,Bug开发紧急修复. 因此,代码臃肿,问题 ...

  10. ios程序启动过程和UIWidnow介绍

    一.iOS程序的完整启动过程(有storyboard) 1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情: ...