leetcode126 Word Ladder II
思路:
宽搜过程中分层记录路径,递归还原。
实现:
- class Solution
- {
- public:
- void getPath(string now, string beginWord, string endWord, vector<string>& buf, unordered_map<string, unordered_set<string>>& par, vector<vector<string>>& ret)
- {
- if (now == beginWord)
- {
- vector<string> tmp(, endWord);
- for (auto it : buf) tmp.push_back(it);
- ret.push_back(tmp);
- reverse(ret.back().begin(), ret.back().end());
- return;
- }
- for (auto it : par[now])
- {
- buf.push_back(it);
- getPath(it, beginWord, endWord, buf, par, ret);
- buf.pop_back();
- }
- }
- vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList)
- {
- unordered_set<string> tmp;
- for (auto it : wordList) tmp.insert(it);
- unordered_map<string, unordered_set<string>> par;
- unordered_set<string> q;
- q.insert(beginWord);
- bool flg = false;
- while (!q.empty())
- {
- unordered_set<string> next;
- for (auto it : q)
- {
- for (int i = ; i < it.length(); i++)
- {
- for (char c = 'a'; c <= 'z'; c++)
- {
- string buf = it;
- if (buf[i] == c) continue;
- buf[i] = c;
- if (!tmp.count(buf)) continue;
- if (!q.count(buf))
- {
- next.insert(buf);
- par[buf].insert(it);
- }
- if (buf == endWord) flg = true;
- }
- }
- }
- for (auto it : q) { tmp.erase(it); }
- q = next;
- if (flg) break;
- }
- vector<vector<string>> ret;
- if (flg)
- {
- vector<string> buf;
- getPath(endWord, beginWord, endWord, buf, par, ret);
- }
- return ret;
- }
- };
leetcode126 Word Ladder II的更多相关文章
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- 何为幻读?MySQL又是如何解决幻读的?
一.什么是幻读 在一次事务里面,多次查询之后,查询的结果集的个数不一致的情况叫做幻读.而多出来或者少的哪一行被叫做 幻行 二.为什么要解决幻读 在高并发数据库系统中,需要保证事务与事务之间的隔离性,还 ...
- 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列
) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...
- git锁和钩子以及图形化界面
1.锁机制 Locking Options 严格锁(strict locking):一个时刻,只有一个人可以占用资源. 乐观锁(optimistic locking):允许多个人同时修改同一文件.乐观 ...
- jQuery源代码解析(1)—— jq基础、data缓存系统
闲话 jquery 的源代码已经到了1.12.0版本号.据官网说1版本号和2版本号若无意外将不再更新,3版本号将做一个架构上大的调整.但预计能兼容IE6-8的.或许这已经是最后的样子了. 我学习jq的 ...
- 【python】一些好的学习网址
http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...
- python中的is判断引用的对象是否一致,==判断值是否相等
python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...
- 关于jQuery写插件及其演示
关于写jQuery插件是非常有必要的.这是前端学习其中必须经过的一个过程 对于初次写插件先想清楚原理 (function($){ $.fn.yourName = function(opt ...
- SPOJ 15. The Shortest Path 最短路径题解
本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...
- jeasyUI treegrid 的 reload 和 getChanges
看上去,treegrid继承自datagrid,因此,datagrid有的,treegrid也会有. 比如说,getChanges,翻看那些网络教程,绝大多数都没提treegrid有这个东东.但是,t ...
- 2016/1/20 笔记 1, 包 引入 static 已经补充到类里 2,继承
继承 1,关键字 extends 2,子类自动继承父类非私有的属性和方法 也叫成员变量 成员方法 3,super代表的是父类 调用父类的方法 默认在构造函数中生成 ...