126. Word Ladder II( Queue; BFS)
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
- Only one letter can be changed at a time
- 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的是
- 入队的元素是从根节点至当前节点的单词数组
- 不能立即删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)的更多相关文章
- 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] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 【leetcode】126. Word Ladder II
题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...
随机推荐
- plsql的快速生成sql语句设置
单 单击tool(工具)->的preferences(首选项) ,进入到首选项页面 在点击user interface 的editor下的autoreplace 的edit按钮 ...
- .net常用正则表达式小结
好久没有些博客了,今天就随便写点工作当中遇到的一些问题.正则表达式估计大家在开发的过程中都会遇到,下面是我平时用到的以及自己整理的一些常用的正则表达式,供大家学习和参考. "^\d+$&qu ...
- Linux 定制X86平台操作系统
/********************************************************************************* * Linux 定制X86平台操作 ...
- Codeforces 1009C: Annoying Present
C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- wpf 客户端【JDAgent桌面助手】开发详解(一)主窗口 圆形菜单
目录区域: wpf 客户端[JDAgent桌面助手]业余开发的终于完工了..晒晒截图wpf 客户端[JDAgent桌面助手]开发详解-开篇 内容区域: 这里开始主窗口 圆形菜单制作的过程,首先请大家看 ...
- Oracle单表去重复(二)
Oracle单表去重 去重有两层含义,一:是记录完全一样.二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而 ...
- volatile的本质
1. 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值在本线程里改变时,会同时把变量的新 ...
- thinkphp3.2.3+smarty解决success调用模板错误心得
最近学习thinkphp上瘾,出现success找不到模板问题,查阅各大神解决方案,分享一下针对新手如何解决该问题,如有不对的地方请大神指正 1.首先修改自己的config文件,添加如下配置代码:// ...
- String..lastIndexOf(".") 返回-1的思考
String s = tableName.substring(tableName.lastIndexOf(".") + 1); 如果有tableName有'.',那么返回正确的截取 ...
- redis位图
<?php function frstr($str){ return str_pad($str,8,'0',STR_PAD_LEFT); } $php=''; $p= frstr(decbin( ...