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 ...
随机推荐
- oracle链接不上的问题
使用plSql连接数据库看看,登录提示如下:ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务. 查了许久的baidu也没有解决问题的方法.想起来看看oracle的服务是否开启,O ...
- Windows Server2008 R2 设置NAT 让Hyper-V连接Internet
1.添加虚拟网卡,设置为内部,并且固定IP地址192.168.1.1 255.255.255.0 此为内网网卡 2.添加服务器角色:DHCP服务器,DNS服务器,网络策略和访问服务 3."网 ...
- VM虚拟机的网卡模式介绍
(1)Bridged方式 用这种方式,虚拟系统的IP可设置成与本机系统在同一网段,虚拟系统相当于网络内的一台.独立的机器,与本机共同插在一个Hub上,网络内其他机器可访问虚拟系统,虚拟系统也可访问网络 ...
- Centos samba install
Ready Change Root Password passwd root 在提示下建立新密码 静态IP vi /etc/sysconfig/network-scripts/ifcfg-eth0 ...
- 绑定服务时什么时候调用onRebind
Serivce中onRebind被调用的时机非常特别,想知道什么时候onRebind被调用,能够接以下的次序来学习.最后自然就明确了! 1. 首先要知道.同一个服务既可能被启动也能够被绑定; 2. S ...
- C++ auto 与 register、static keyword 浅析
[register/auto的比較分析] #include <iostream> using namespace std; int main(){ int i,sum=0; for(i=0 ...
- C++进阶之虚函数表
C++通过继承(inheritance)和虚函数(virtual function)来实现多态性.所谓多态,简单地说就是,将基类的指针或引用绑定到子类的实例,然后通过基类的指针或引用调用实际子类的成员 ...
- iOS UITableViewCell 几个方法的优先级
#第一组 - (void)setDataDict:(NSDictionary *)dataDict;这种方法优先运行 - (id)initWithStyle:(UITableViewCellSty ...
- javascript总结03
- Linux/Android——Input系统之frameworks层InputManagerService (六)【转】
本文转载自:http://blog.csdn.net/u013491946/article/details/72638954 版权声明:免责声明: 本人在此发文(包括但不限于汉字.拼音.拉丁字母)均为 ...