leetcode25word-ladder
题目描述
- 每一次转换只能改变一个单词
- 每一个中间词都必须存在单词字典当中
shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start ="hit"
end ="cog"
dict =["hot","dot","dog","lot","log"]
As one shortest transformation is"hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length5.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
queue<string>Q;
Q.push(start);
int res=1;
while (!Q.empty()){
int qsize=Q.size();
while (qsize--){
string cur_front=Q.front();
Q.pop();
int size=cur_front.size();
for (int i=0;i<size;i++)
{
char ch=cur_front[i];
for (int j=0;j<26;j++){
cur_front[i]='a'+j;
if (cur_front==end) return res+1;
if (dict.find(cur_front)!=dict.end())
{
Q.push(cur_front);
dict.erase(cur_front);
}
}
cur_front[i]=ch;
}
}
res++;
}
return 0;
}
};
leetcode25word-ladder的更多相关文章
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- Jmeter之『如果(If)控制器』
判断方法 ${__jexl3("${projectName}"=="${targetDir}",)} ${__groovy("${projectNam ...
- SQL数据库删除和还原的时候提示 被占用 记录一下
设置离线 1)ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE设置在线 2)ALTER DATABASE [datebase] ...
- ANOI 2009 【同类分布】
好累啊啊啊~~~~~~,刷了一天的题了,嗯,再写两篇题解我就去颓Slay... 思路分析: 刚刚我们讲了数位DP,现在就感受一下吧.(其实我也就只敢做做安徽的题,四川的数位DP想都不敢想) 嗯好,我们 ...
- JS原生练习
1.输出1-10000之间的数 <script> for(i=1;i<=10000;i++) { document.write(i + "<br>") ...
- jq显示数据在kindeditor
1,定义编辑器的变量为全局变量 2,将数据显示到kindeditor 在我自己这里_下划线相当于数据,也就是将数据显示在kindeditor 中的textarea中 3,jquery获取kinde ...
- cmd备份数据库,还原数据库,仅限于php
第一:先备份数据库 1.进入cmd(黑盒子) 2.进入phpstudy所在的盘 3.cd E: 3.cd phpstudy; 4.cd PHPTutorial 5.cd mysql; 6.cd bin ...
- Go strconv包
strconv包 该包主要实现基本数据类型与其字符串表示的转换. 常用函数为Atoi().Itia().parse系列.format系列.append系列. 更多函数请查看官方文档. string与i ...
- python反序列化学习记录
pickle与序列化和反序列化 官方文档 模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化. "pickling" 是将 Python 对象及其所拥 ...
- MySQL - 常用三种数据库存储引擎
数据库存储引擎:是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以获得特 ...
- 多测师讲解python _函数中参数__高级讲师肖sir
函数中讲解参数: 形参和实参的认识 函数无参数的调用 函数单个参数的调用 函数多个参数的调用 # #调试函数给默认参数传新值,则函数使用新值 # 注意:当多种参数同时出现在函数中,默认参数要放在最后的 ...