【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 ...
随机推荐
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...
- “Transaction rolled back because it has been marked as rollback-only”
spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到“Transaction rolled b ...
- Android Studio-设置放大代码编辑区
原有的快捷键是ctrl+shift+F12,现在我修改成了Ctrl+M.
- javascript DOM操作之 querySelector,querySelectorAll
javascript DOM操作之 querySelector,querySelectorAll
- (准备写)URAL1824 Ifrit Bomber 题解
http://acm.timus.ru/problem.aspx?space=1&num=1824 1824. Ifrit Bomber Time limit: 0.5 second Memo ...
- 在windows7 上安装 Sublime Text 3 及其插件
1.下载地址:http://www.sublimetext.com/3 请根据你的平台,选择适当的安装版本 安装完毕后,设定TAB键为4个空格( Preferences——>Setings-Us ...
- Ali相关面试题
接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...
- css 设计总结
一.背景图片的拉伸: backgroud-size 说明: http://www.w3school.com.cn/cssref/pr_background-size.asp 效果: http:// ...
- php中向mysql插入数据
$sql='insert into news(title,subtitle,source,publishtime,content,image,author) values("'.unico ...
- ThinkPHP框架表单验证
对注册到test表的表单进行验证 在注册之前要对表单进行验证: 用户名非空验证,两次输入密码必须一致即相等验证,年龄在18~50之间即范围验证,邮箱格式正则验证. 自动验证是ThinkPHP模型层提供 ...