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. 探究js正则匹配方法:match和exec

    match是字符串方法,写法为:str.match(reg) exec是正则表达式方法,写法为:reg.exec(str) match和exec在匹配成功时返回的都是数组,在没有匹配上时返回的都是nu ...

  2. FOR UPDATE

    1. for update的使用场景 `如果遇到存在高并发并且对于数据的准确性很有要求的场景,是需要了解和使用for update的.    比如涉及到金钱.库存等.一般这些操作都是很长一串并且是开启 ...

  3. show()是非模式窗体. showDialog()是模式窗体.

    show()仅仅是显示出来窗口界面而已```也就是和你执行的结果在同一窗口显示```所显示的窗口可以在后台运行```而showDialog()是一个对话框窗口界面```执行结果以新窗口界面出现```不 ...

  4. js字符串的裁剪

    一.JavaScript字符串的处理方法 1.split()  功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子:   str=”jpg|bmp|gif|ico|png”; arr=str. ...

  5. BZOJ1568: [JSOI2008]Blue Mary开公司【李超树】

    Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词"Query"或"Project". 若单词为Q ...

  6. 白帽子讲web安全——一个安全解决方案的诞生细节

    1.白帽子:做安全的人.主要做的事,防御,是制定一套解决攻击的方案.而不是只是解决某个漏洞. 2.黑帽子:现在说的黑客.让web变的不安全的人.利用漏洞获取特权.主要做的事,攻击,组合各种方法利用漏洞 ...

  7. 解决crontab不加载环境变量问题

    公司需要做异构库数据同步,由于之前实际使用过,且字段类型也兼容,满足业务场景,使用了阿里开源数据同步工具:datax,服务器上crontab定时脚本执行. 由于crontab只加载/ect/envir ...

  8. 开始SDK之旅-入门2-集成流程图、轨迹图到系统

    http://bbs.ccflow.org/showtopic-2562.aspx 经测试,基本可用,还需增加 WF/Admin/pub.ascx 首先你得先理解流程图的数据的获取方式,其他的就很容易 ...

  9. 大型发布会现场的 Wi-Fi 应该如何搭建(密集人群部署wifi抗干扰)?

    原文连接: http://www.zhihu.com/question/20890194 WiFi网络的部署要远远比一般人想象的复杂,不是说放上几十个AP带宽就自动增加几十倍,恰恰相反,简单放几十个A ...

  10. mybatis 高级映射 简单例子

    1.建表 DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `gender` ) NOT NULL, `name` ) NOT NULL, `id` ...