127. Word Ladder (Tree, Queue; WFS)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence 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"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
思路:
Step1:把本题看成是树状结构
Step2:通过两个队列,实现层次搜索
class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
queue<string> queue_to_push;
queue<string> queue_to_pop;
bool endFlag = false;
string curStr;
int level = ;
char tmp; queue_to_pop.push(beginWord);
wordList.erase(beginWord); //if beginWord is in the dict, it should be erased to ensure shortest path
while(!queue_to_pop.empty()){
curStr = queue_to_pop.front();
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){
return level+;
}
else if(wordList.count(curStr)>){ //occur in the dict
queue_to_push.push(curStr);
wordList.erase(curStr);
}
curStr[i] = tmp; //back track
}
} if(queue_to_pop.empty()){//move to next level
swap(queue_to_pop, queue_to_push);
level++;
}
}
return ;
}
};
127. Word Ladder (Tree, Queue; WFS)的更多相关文章
- 127. Word Ladder(M)
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- 126. Word Ladder II( Queue; BFS)
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- SharePoint 2013的100个新功能之开发
一:SharePoint应用 SharePoint 2013引入了云应用模型来供用户创建应用.SharePoint应用是独立的功能整合,扩展了SharePoint网站的功能.应用可以包含SharePo ...
- Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库
最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...
- I.MX6 GPS Android HAL Framework 调试
I.MX6 GPS Android HAL Framework 调试 一.参考文章: android4. GPS定位问题 http://blog.csdn.net/bzw073/article/det ...
- Python 使用Microsoft SQL Server数据库
软件环境: Windows 7 32bit Python 3.6 Download https://www.python.org/downloads/ 默认安装,并添加环境变量,一路Next ... ...
- Jenkins搭建.NET自动编译发布远程环境
继上一篇文章Jenkins搭建.NET自动编译发布本地环境 发布到本地成功后,接下来配置发布到远程环境. Build配置——发布到远程 根据前面VS中发布项目,生成的CustomProfile2 来配 ...
- HDU 4123 Bob’s Race(RMQ)
题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...
- 抽取JDBCTemplate
抽取JDBCTemplate 为了解决DAO实现类中代码的重复问题,另外使得代码更通用,所以抽取一个公共的模板,使其更具有通用性. DAO实现类的代码 public class StudentDAOI ...
- Oracle使用startup与startup force启动的区别
1. startup 就是正常启动数据库,没什么好说的. 2. startup force 是shutdown abort + startup的组合,即强制关闭数据库+ 正常启动数据库,想快速重启数据 ...
- 给VS类文件添加默认头注释
找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...
- IE11浏览器卸载
点击卸载程序,然后选择查看已安装的更新. 在当前安装的更新里找到IE11的更新,然后直接右击卸载:这里告诉大家一小窍门哈,我们在搜索栏输入IE就会查找更新啦,不用一个一个去找的哦. 卸载完我们重启一下 ...