Word Ladder——LeetCode
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.
题目大意:给定两个等长的单词,各一个字典,每次只能变换一个字符得到中间词,此中间词必须在字典里存在,求从一个单词变换到另一个单词最短需要多少步。
解题思路:说实话这题一开始觉得不知道从哪儿下手,后来看了下tag是BFS,才有了思路,从字典里寻找与起始单词距离为1的所有单词,加入bfs队列,然后当队列非空,取出队列中的单词,查找在字典里的所有与之距离为1的单词,直到找到结束单词或者全部遍历完没有合法解,返回0;
Talk is cheap>>
public int ladderLength(String start, String end, Set<String> dict) {
if (transInOne(start, end)) {
return 2;
}
Queue<String> queue = new ArrayDeque<>();
queue.add(start);
int length = 1;
while (!queue.isEmpty()) {
length++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String toSearch = queue.poll();
if (transInOne(toSearch, end)) {
return length;
}
for (Iterator<String> iterator = dict.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
if (transInOne(toSearch, next)) {
queue.offer(next);
iterator.remove();
}
}
}
}
return 0;
} private boolean transInOne(String start, String end) {
int i = 0;
int res = 0;
while (i < start.length()) {
if (start.charAt(i) != end.charAt(i)) {
res++;
if (res > 1)
return false;
}
i++;
}
return true;
}
Word Ladder——LeetCode的更多相关文章
- Word Ladder - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Word Ladder - LeetCode 注意点 每一个变化的字母都要在wordList中 解法 解法一:bfs.类似走迷宫,有26个方向(即26个字 ...
- Word Ladder leetcode java
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- [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 OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- [Leetcode Week5]Word Ladder
Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...
随机推荐
- 利用System V消息队列实现回射客户/服务器
一.介绍 在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如: 1. 服务器必须启动之时,客户端才能连上服务端,并 ...
- MVC模式实现登录以及增删改查之登录(一)
我在这里用的不是maven项目,用的一般的web项目,所以需要用到的架包需要自己去下载添加,在项目中,一定注意环境的配置,我用的是jre1.7 1 新建项目 2 建立好MVC的管理包,导入对应的架 ...
- 初学 Canvas <第一篇-基础篇>
本文摘自:兴趣部落大神(为你一生画眉)-讲一讲canvas究竟是个啥? HTML5 的标准已经出来好久了,但是似乎其中的 Canvas 现在并没有在太多的地方用到.一个很重要的原因是,Canvas 的 ...
- 如何解决eclipse上的Android程序“Please ensure that adb is correctly located at 'D:\eclipse\sdk\platform-tools\adb.exe' and can be executed.”小问题?
首先,把运行的Android模拟器和eclipse一块儿关了, 然后win+R,cmd, 下面输入adb kill_server 再输入adb start_server 之后重新运行项目,不出意外的话 ...
- 从C到汇编:栈是计算机工作的基础
作者:r1ce 原创作品转载请注明出处 <Linux内核分析> MOOC课程http://mooc.study.163.com/course/U ...
- 模板-->单变元模线性方程
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 extend_gcd模板 poj_2115_C Looooops,my_ac_code 简单的测试 None 代码模板 / ...
- Android 静默安装/后台安装
Android实现静默安装其实很简单,今天在网上找资料找半天都说的很复杂,什么需要系统安装权限.调用系统隐藏的api.需要系统环境下编译.需要跟systemUI同进程什么的.我不知道他们真的实现了静默 ...
- 【转】HttpServlet详解
[转]HttpServlet详解 Servlet的框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax.servlet包中定义了所有的Servl ...
- Android开发手记(24) Log的使用及颜色的更改
在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用.本文 ...
- 【转】 iOS Provisioning Profile(Certificate)与Code Signing详解
原文:http://blog.csdn.net/phunxm/article/details/42685597 引言 关于开发证书配置(Certificates & Identifiers & ...