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 ...
随机推荐
- Alpha冲刺一 (10/10)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10034872.html 作业博客:https://edu.cnblogs.com/campus ...
- SQL中注意数据类型对性能的影响
在数据存储的时候有时我们不太注意字符编码对性能影响,但小问题往往造成很大的影响.在数据量小的时候感觉不出来,一旦上到百万级以上的时候就非常明显了 看下面两个SQL语句 ---SQL1 SELECT * ...
- ubuntu:NVIDIA设置性能模式,以降低CPU使用、温度
NVIDIA设置性能模式,以降低CPU使用.温度 ubuntu安装完NVIDIA显卡驱动后 终端输入 nvidia-settings 选择OpenGL Settings->Image Setti ...
- python中多进程
多进程 什么是进程 进程:正在进行的一个过程或者说一个任务,而负责执行任务的是CPU. 进程和程序的区别 程序仅仅是一堆代码而已,而进程指的是程序的运行过程. 举例 想象以为有着一手好厨艺的科学家肖亚 ...
- 微信逆向工程之远程操作Mac
远程控制指令: (功能-指令-是否开启) macbook控制: 屏幕保护-ScreenSave-开启 锁屏-LockScreen-开启 休眠-Sleep-开启 关机-Shutdown-开启 重启-Re ...
- multiprocessing创建自定义进程类
1.继承Process2.编写自己的__init__,同时加载父类init方法3.重写run方法,可以通过生成的对象调用start自动运行 from multiprocessing import Pr ...
- BZOJ4978: [Lydsy1708月赛]泛化物品(乱搞)
4978: [Lydsy1708月赛]泛化物品 Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 220 Solved: 70[Submit][Statu ...
- python3 scrapy 使用selenium 模拟浏览器操作
零. 在用scrapy爬取数据中,有写是通过js返回的数据,如果我们每个都要获取,那就会相当麻烦,而且查看源码也看不到数据的,所以能不能像浏览器一样去操作他呢? 所以有了-> Selenium ...
- 常见web安全攻防总结
Web 安全的对于 Web 从业人员来说是一个非常重要的课题 , 所以在这里总结一下 Web 相关的安全攻防知识,希望以后不要再踩雷,也希望对看到这篇文章的同学有所帮助.今天这边文章主要的内容就是分析 ...
- ambassador 学习七 Mapping说明
mapping 通过rest 资源与k8s 的service进行关联,ambassador 必须有一个或者多个提供访问servide 的mapping定义 mapping 可以包含的配置 rewrit ...