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 ...
随机推荐
- 关于父类私有属性在子类构造函数中super调用的解释
package test; public class Car { private int carMoney; //汽车租金 private String carName; //汽车名字 private ...
- JDK5的新特性:泛型、可变参数、静态导入
数组可以在创建的时候就指定存放的数据类型,这样放入不同类型的时候就会发生编译错误. 而集合却可以存储多种不同类型,这样的话如果是遍历的时候在集合中装了好多不同的数据类型的时候,十分容易发生类型转换错误 ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 10
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...
- Git Error: warning: refname 'origin/branch-name' is ambiguous.
When this happened, it created the file .git/refs/heads/origin/branch-name. So, I just deleted the f ...
- js网页 唤醒支付宝
过渡页: <script> window.location.href = 'alipays://platformapi/startApp?appId=10000011&url=al ...
- Alone
---恢复内容开始--- 出处:皮皮bloghttp://blog.csdn.net/pipisorry/article/details/50709014 coding.net: 国内较好的代码托管平 ...
- Qt UI界面改了,但UI界面不更新
/**************************************************************************** * Qt UI界面改了,但UI界面不更新 * ...
- 每天一个linux命令(性能、优化):【转载】iostat命令
Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.同 ...
- localhost能连接websocket 127.0.0.1 不能连接问题?
最近开发中遇到一个问题,就是有的浏览器电脑能连接websocket , 有的不能 , 有的能用localhost连接,有的能用127.0.0.1连接,这个问题很奇怪 提供一个很好测试websocket ...
- Spring boot启动原理
1.入口类 /** * springboot应用的启动入口 */ @RestController @SpringBootApplication public class SampleApplicati ...