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 ...
随机推荐
- 用 Unity 和 HTC Vive 实现高级 VR 机制(1)
原文:Advanced VR Mechanics With Unity and the HTC Vive Part 1 作者:Eric Van de Kerckhove 译者:kmyhy VR 从来没 ...
- CI框架的引导流程以及基准测试类
一[CI]框架的引导流程了解一下,并掌握如何新增自己的扩展类库 http://www.cnblogs.com/ohmygirl/p/CIRead-4.html // CI框架源码阅读笔记4 引导文 ...
- 20155315 2016-2017-2 《Java程序设计》第八周学习总结
教材学习内容总结 第14章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区(Buffer)容量,在缓冲区中对感兴趣的数据区块进 ...
- 20155234 2016-2017-2 《Java程序设计》第8周学习总结
20155234 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 java.util.loggging包提供了日志功能相关类与接口. 使用日志的起点是Logg ...
- swift3.0 代码创建经典界面的九宫图--优化篇
在上一篇只是简单实现了九宫图效果,本章需要形成APP界面九宫图效果 override func viewDidLoad() { super.viewDidLoad() createnine() } / ...
- DesignPattern(三)结构型模式(上)
结构型模式 结构型模式,顾名思义讨论的是类和对象的结构 ,主要用来处理类或对象的组合.它包括两种类型,一是类结构型模式,指的是采用继承机制来组合接口或实现:二是对象结构型模式,指的是通过组合对象的方式 ...
- 进程的proc文件系统信息
一.实验代码 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include &l ...
- nats 学习 request/reply 模式基本使用
nats 一个云原生的消息系统,使用简单,客户端丰富,支持的模式是pub/sub 但是集成比较灵活,可以支持loadblance, request/reply pub/sub 代码演示的是reques ...
- Bootstrap-CL:警告
ylbtech-Bootstrap-CL:警告 1.返回顶部 1. Bootstrap 警告(Alerts) 本章将讲解警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警 ...
- Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次)
ylbtech-Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次) 1.返回顶部 1. 大家好! 欢迎大家参加蚂蚁金融云 即将在2018年1月17日到1月18日 在北京 环球 ...