博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Word Ladder II
阅读量:5822 次
发布时间:2019-06-18

本文共 4010 字,大约阅读时间需要 13 分钟。

This is a tough problem! I read some solutions from the web. provides a one-end BFS solution. The code is reorganized as follows.

1 class Solution { 2 public: 3     vector
> findLadders(string start, string end, unordered_set
&dict) { 4 dict.insert(end); 5 unordered_set
current; 6 unordered_set
next; 7 current.insert(start); 8 ladder.push_back(end); 9 while (true) {10 for (string cur : current)11 dict.erase(cur);12 for (string cur : current)13 findChildren(cur, next, dict);14 if (next.empty()) return ladders;15 if (next.find(end) != next.end()) {16 genLadders(start, end);17 return ladders;18 }19 current.clear();20 swap(current, next);21 }22 }23 private:24 unordered_map
> ancestors;25 vector
ladder;26 vector
> ladders;27 28 void findChildren(string& word, unordered_set
& next, unordered_set
& dict) {29 int len = word.length();30 string ancestor = word;31 for (int p = 0; p < len; p++) {32 char letter = word[p];33 for (int i = 0; i < 26; i++) {34 word[p] = 'a' + i;35 if (dict.find(word) != dict.end()) {36 next.insert(word);37 ancestors[word].push_back(ancestor);38 }39 }40 word[p] = letter;41 }42 }43 44 void genLadders(string& start, string& end) {45 if (start == end) {46 reverse(ladder.begin(), ladder.end());47 ladders.push_back(ladder);48 reverse(ladder.begin(), ladder.end());49 return;50 }51 for (string ancestor : ancestors[end]) {52 ladder.push_back(ancestor);53 genLadders(start, ancestor);54 ladder.pop_back();55 }56 }57 };

shares a two-end BFS solution, which is much faster! I have also rewritten that code below. 

1 class Solution { 2 public: 3     vector
> findLadders(string start, string end, unordered_set
&dict) { 4 vector
ladder; 5 vector
> ladders; 6 ladder.push_back(start); 7 unordered_set
startWords; 8 unordered_set
endWords; 9 startWords.insert(start);10 endWords.insert(end);11 unordered_map
> children;12 bool flip = true;13 if (searchLadders(startWords, endWords, children, dict, ladder, ladders, flip))14 genLadders(start, end, children, ladder, ladders);15 return ladders;16 }17 private:18 bool searchLadders(unordered_set
& startWords, unordered_set
& endWords,19 unordered_map
>& children, unordered_set
& dict,20 vector
& ladder, vector
>& ladders, bool& flip) {21 flip = !flip;22 if (startWords.empty()) return false;23 if (startWords.size() > endWords.size())24 return searchLadders(endWords, startWords, children, dict, ladder, ladders, flip);25 for (string word : startWords) dict.erase(word);26 for (string word : endWords) dict.erase(word);27 unordered_set
intermediate;28 bool done = false;29 for (string word : startWords) {30 string temp = word;31 int len = word.length();32 for (int p = 0; p < len; p++) {33 char letter = word[p];34 for (int i = 0; i < 26; i++) {35 word[p] = 'a' + i;36 if (endWords.find(word) != endWords.end()) {37 done = true;38 flip ? children[word].push_back(temp) : children[temp].push_back(word);39 }40 else if (!done && dict.find(word) != dict.end()) {41 intermediate.insert(word);42 flip ? children[word].push_back(temp) : children[temp].push_back(word);43 }44 }45 word[p] = letter;46 }47 }48 return done || searchLadders(endWords, intermediate, children, dict, ladder, ladders, flip);49 }50 void genLadders(string& start, string& end, unordered_map
>& children,51 vector
& ladder, vector
>& ladders) {52 if (start == end) {53 ladders.push_back(ladder);54 return;55 }56 for (string child : children[start]) {57 ladder.push_back(child);58 genLadders(child, end, children, ladder, ladders);59 ladder.pop_back();60 }61 }62 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4618900.html

你可能感兴趣的文章
vim中文手册
查看>>
DB2 创建 TOOLSDB数据库
查看>>
终于把空岛的故事看完了
查看>>
深入浅出 Barriers 实现(一)
查看>>
VS2015 + Cordova Html5开发使用Crosswalk Web引擎
查看>>
Lua语言如何调用自己编写的C DLL -- 转
查看>>
Embedded linux primer
查看>>
stm8s + si4463 寄存器配置
查看>>
C++ STL标准模板库(list)
查看>>
做一个通用的XML序列化,支持所有类型
查看>>
winform datagridview 设置单元格为只读属性。
查看>>
JDOM入门实例:读取与创建xml文档
查看>>
读书笔记《集体智慧编程》Chapter 8 : Build Price Models
查看>>
完美解决PHP中文乱码问题
查看>>
似是而非的k=sqrt(n)
查看>>
HDOJ 1106
查看>>
链接脚本使用一例2---将二进制文件 如图片、MP3音乐、词典一类的东西作为目标文件中的一个段...
查看>>
[问题2014A01] 复旦高等代数 I(14级)每周一题(第三教学周)
查看>>
Ewebeditor最新漏洞和漏洞指数
查看>>
浏览器恶意软件
查看>>