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 ...
随机推荐
- 实现ARC文件与MRC文件互转,和混合使用。
这段时间做项目是以MRC为主的 但是某些第三方现在都只能支持ARC了 找到了这篇文章 可谓是帮了大忙 亲测完全可用喔:-O 如何在未使用arc的工程中引入一个使用了arc特性的文件,如何在arc工程中 ...
- Macos Coco2d-x Android开发
1. 在装好环境 2. cocos new [-h] [-p PACKAGE_NAME] -l {cpp,lua,js} [-d DIRECTORY] [-t TEMPLATE_NAME] [--io ...
- 模拟电路"虚短" & "虚断"
<虚短 & 虚断> 运算放大器组成的电路五花八门,令人眼花瞭乱,是模拟电路中学习的重点.遍观所有模拟电子技朮的书籍和课程,在介绍运算放大器电路的时候,无非是先给电路来个定性,比如这 ...
- MeasureSpec
在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...
- 登录模块需要用到session留底
<% HttpSession sessions=request.getSession(); Object sessionValues=sessions.getAttribute("ui ...
- ASPNET5应用程序启动
1. 启动类 在asp.net5当中,Startup类提供应用程序的入口,对所有应用程序是必须的.争对特定环境的配置的starup class和方法也是有可能的, 但是,不管怎样, 一个Startup ...
- Android开发手记(21) 遍历文件夹
我们在遍历文件夹的时候由于涉及到SD卡相关操作,所以我们需要添加如下权限: <uses-permission android:name="android.permission.WRIT ...
- iOS开发实现登陆
Assumption假设:iOS端加载Web页,然后用户输入用户名密码登陆,WebServer会把用户登陆信息记载在Cookie.那么iOS客户端如何取到Cookie中的登陆信息. 客户端监听 NSH ...
- 重新开始学习javase_隐藏实施过程
一.隐藏实施过程 对于隐藏实施过程,thinking in java中讲了很好,无非就是一个好的程序尽量做到,对外公开的程序,即使内部程序发生变动,也不会影响这些公开的服务的使用 类的导入java中的 ...
- 保垒机SSH登录脚本
线上服务器一般都会有一个保垒机,我们登录线上服务器的时候都要通过这个堡垒机来实现登录,这样我们就要维护很多线上服务器的ip,很麻烦. 所以写了一个脚本用来便捷的登录各个服务器,可以把这个脚本放到跳板机 ...