本题有几个注意点:

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. linux6 安装oracle11g

    转自:http://blog.csdn.net/nhm_lxy/article/details/37813789 安装环境:    虚拟机VMware Workstation 10.0 操作系统:   ...

  2. CF 414B Mashmokh and ACM 动态规划

    题意: 给你两个数n和k.求满足以下条件的数列有多少个. 这个数列的长度是k: b[1], b[2], ……, b[k]. 并且 b[1] <= b[2] <= …… <= b[k] ...

  3. 【iOS开发-34】自己主动释放池@autoreleasepool的使用注意事项以及ARC机制——面试必考内容

    自己主动释放池@autorelease面试频率可能会吧release还要高. (1)在自己主动释放池@autoreleasepool{}中alloc一个对象后(如p1).仍然须要用[p1 autore ...

  4. 关于HttpClient模拟浏览器请求的參数乱码问题解决方式

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44407297 http://www.llwjy.com/blogdetail/9 ...

  5. 小白学开发(iOS)OC_ 经常使用结构体(2015-08-14)

    // //  main.m //  经常使用结构体 // //  Created by admin on 15/8/13. //  Copyright (c) 2015年 admin. All rig ...

  6. CentOS7 启动[root@localhost ~]# systemctl start docker Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for de

    1).在linux虚拟机上安装docker步骤:1.检查内核版本,必须是3.10及以上uname ‐r2.安装dockeryum install docker3.输入y确认安装4.启动docker[r ...

  7. JAVA使用Gson解析json数据,实例

    封装类Attribute: public class Attribute { private int id; private String name; private int age; public ...

  8. 使用iVMS-4200 存储录像数据时的设置

    1.安装软件时,选择:存储服务器 2.对存储服务器进行配置,具体配置见 配置手册.

  9. https soap链接示例

    1.先安装soap扩展sudo yum install php-soap 2.安装openssL 3.function  issure($sn){//通过soap链接接口  进行确认是否是正确的sn码 ...

  10. 【VC++学习笔记五】SDI|MDI的全屏显示

    一.Mainframe中添加一个记录是否全屏状态的变量BOOL m_bFullScreen. 二.工具栏添加一个按钮,进行全屏的操作,响应事件函数写在Mainframe中. 三.在响应函数中,添加如下 ...