【leetcode】Word Ladder
Word Ladder
Total Accepted: 24823 Total Submissions: 135014My Submissions
Given two words (start and end), and a dictionary, find the length of 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 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.
int ladderLength(string start, string end, unordered_set<string> &dict) {
int n=start.size();
if(n<||n!=end.size())
{
return ;
}
if(start==end)
{
return ;
} int level=;
queue<string> q;
q.push(start);
//count用来记录每一个深度的元素的个数
int count=;
while()
{
start=q.front();
q.pop();
count--;
for(int i=;i<start.length();i++)
{
string ori=start;
//每次修改一个字符,看是否在字典中能找到
for(char ch='a';ch<='z';ch++)
{
if(start[i]==ch)continue; start[i]=ch;
if(start==end) return level;
//如果能找到,则用queue记录下下一层深度的元素
if(dict.find(start)!=dict.end())
{
dict.erase(start);
q.push(start);
}
start=ori;
}
} //没有下一层深度了,或者dict已经为空
if(q.empty()||dict.empty())
{
break;
} //count为0,说明该level的元素已经被遍历完了
if(count==)
{
level++;
count=q.size();
}
}
return ;
}
【leetcode】Word Ladder的更多相关文章
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【leetcode】Word Ladder (hard) ★
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
随机推荐
- JS 根据特定URL获取ID数组
工作中遇到的问题 咱是菜鸟 有更好的方法 求大神看到的指点 事情经过主要是后台返回商品ID 和 商品数量 然后做个卖光的遮罩 这样的效果 结果 后台返回的ID 数组不是后台输入的时候的排序 也就是 ...
- ElasticSearch之一——索引
ElasticSearch索引 ElasticSearch 是一个分布式可扩展的实时搜索引擎,它建立在开源搜索引擎框架Apache Lucene基础上.ElasticSearch 不但包括了全文搜索功 ...
- Storm:最火的流式处理框架
伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的当然是这 ...
- angularjs之自己定义指令篇
1>指令基础知识 directive() 参考资料 http://www.tuicool.com/articles/aAveEj http://damoqiongqiu.iteye.com/bl ...
- 【8-16】Android学习笔记01
Android目录树 Activity 生命周期 创建一个 Activity 在 android 中创建一个 Activity 要编写一个继承自 android.app.Activity的 Java ...
- thinkphp-许愿墙-3
用jquery写异步传递的时候, 首先要判断表单中的输入是否为空: 如果有多个输入项, 应该, 分别的, 一步一步的来判断是否为空, 而不是用 and / or来复合判断! 同时如果为空, 应该将它设 ...
- vim 打开Linux下文件每一行后面都有^M的样式
由于服务器不是我一个人在操作,在修改apache配置文件时发现了一个很奇怪的问题,vim编辑打开配置文件发现后面都有一个^M的标记 虽然不会影响服务的运行,但总感觉不对劲,所以在此我尝试用替换的方式来 ...
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
- Linux之服务器时间同步
Linux时间同步(把nameNode1作为时间同步服务器) 设置时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 联网情况:ntpdate ...
- android ListView嵌套GridView显示不全问题
只需重写GridView即可:public class MyGridView extends GridView{ public MyGridView(android.content.Context c ...