Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Return

  [
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

思路:仍然使用两个队列做WFS, 不同于I的是

  1. 入队的元素是从根节点至当前节点的单词数组
  2. 不能立即删dict中的元素,因为该元素可能在同级的其他节点中存在
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
queue<vector<string>> queue_to_push;
queue<vector<string>> queue_to_pop;
set<string> flag;
set<string>::iterator it;
bool endFlag = false;
string curStr;
vector<string> curVec;
vector<vector<string>> ret;
char tmp; curVec.push_back(beginWord);
queue_to_pop.push(curVec);
wordList.erase(beginWord); //if beginWord is in the dict, it should be erased to ensure shortest path
while(!queue_to_pop.empty()){
curVec = queue_to_pop.front();
curStr = curVec[curVec.size()-];
queue_to_pop.pop(); //find one letter transformation
for(int i = ; i < curStr.length(); i++){ //traverse each letter in the word
for(int j = 'a'; j <= 'z'; j++){ //traverse letters to replace
if(curStr[i]==j) continue; //ignore itself
tmp = curStr[i];
curStr[i]=j;
if(curStr == endWord){
curVec.push_back(curStr);
ret.push_back(curVec);
endFlag = true;
curVec.pop_back(); //back track
}
else if(!endFlag && wordList.count(curStr)>){ //occur in the dict
flag.insert(curStr);
curVec.push_back(curStr);
queue_to_push.push(curVec);
curVec.pop_back(); //back track
}
curStr[i] = tmp; //back track
}
} if(queue_to_pop.empty()){//move to next level
if(endFlag){ //terminate
break; //Maybe there's no such path, so we return uniformaly at the end
}
for(it=flag.begin(); it != flag.end();it++){ //erase the word occurred in current level from the dict
wordList.erase(*it);
}
swap(queue_to_pop, queue_to_push); //maybe there's no such path, then endFlag = false, queue_to_push empty, so we should check if queue_to_pop is empty in the next while
flag.clear();
}
}
return ret;
}
};

126. Word Ladder II( Queue; BFS)的更多相关文章

  1. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  4. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  5. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  6. 【leetcode】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

  7. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  9. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

    https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...

随机推荐

  1. Linux运维学习笔记-网络安全等级保护

    网络安全等级保护简介与作用: 验证信息系统是否满足相应安全保护等级的一个过程. 对不同信息系统分等级进行保护.

  2. 每天一个linux命令(性能、优化):【转载】iostat命令

    Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.同 ...

  3. InputStream,InputStreamReader和Reader的关系

    InputStream:得到的是字节输入流,InputStream.read("filename")之后,得到字节流 Reader:读取的是字符流 InputStreamReade ...

  4. 关于const 和指针

    这个很久之前就很困扰的问题,现在再理一下: 1,指向const对象的指针 >C++强制要求指向const对象的指针也必须具有const特性!!!也就是不能把一个const对象的地址赋给一个非co ...

  5. [CF321E]Ciel and Gondolas&&[BZOJ5311]贞鱼

    codeforces bzoj description 有\(n\)个人要坐\(k\)辆车.如果第\(i\)个人和第\(j\)个人同坐一辆车,就会产生\(w_{i,j}\)的代价. 求最小化代价.\( ...

  6. Spring boot启动原理

    1.入口类 /** * springboot应用的启动入口 */ @RestController @SpringBootApplication public class SampleApplicati ...

  7. PHP安全性考虑

    用户提交的数据 很多PHP 程序所存在的重大弱点并不是PHP 语言本身的问题,而是编程者的安全意识不高而导致的.因此,必须时时注意每一段代码可能存在的问题,去发现非正确数据提交时可能造成的影响. 例子 ...

  8. 在Chrome调试器中引入jQuery

    在Console中输入以下代码并回车,Console显示"function (a,b){return new m.fn.init(a,b)}"说明导入成功,就可以在Console中 ...

  9. 使用sublime text3手动安装插件

    众所周知,由于网络问题,使用install package可能会连不上服务器,这时候就不得不手动安装插件了. 其实sublime安装插件的原理就是当你搜索一个插件并安装的时候,它回去github找到这 ...

  10. charles手机抓包配置-1

    1.下载和安装Charles 下载和安装Charles软件.安装前要先安装Java运行环境,因为Charles是Java写的. 自己百度云上有破解版 2.安装电脑的Charles证书,对证书进行信任设 ...