Word Ladder leetcode java
题目:
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.
题解:
这道题是套用BFS同时也利用BFS能寻找最短路径的特性来解决问题。
把每个单词作为一个node进行BFS。当取得当前字符串时,对他的每一位字符进行从a~z的替换,如果在字典里面,就入队,并将下层count++,并且为了避免环路,需把在字典里检测到的单词从字典里删除。这样对于当前字符串的每一位字符安装a~z替换后,在queue中的单词就作为下一层需要遍历的单词了。
正因为BFS能够把一层所有可能性都遍历了,所以就保证了一旦找到一个单词equals(end),那么return的路径肯定是最短的。
像给的例子 start = hit,end = cog,dict = [hot, dot, dog, lot, log]
按照上述解题思路的走法就是:
level = 1 hit dict = [hot, dot, dog, lot, log]
ait bit cit ... xit yit zit , hat hbt hct ... hot ... hxt hyt hzt , hia hib hic ... hix hiy hiz
level = 2 hot dict = [dot, dog, lot, log]
aot bot cot dot ... lot ... xot yot zot,hat hbt hct ... hxt hyt hzt,hoa hob hoc ... hox hoy hoz
level = 3 wordQueue.add(start);
String word = wordQueue.poll();
curnum--;
wordunit[i] = j;
String temp =
wordQueue.add(temp);
nextnum++;
dict.remove(temp);
}
}
}
curnum = nextnum;
nextnum = 0;
level++;
}
}
}
Word Ladder leetcode java的更多相关文章
- Word Ladder - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Word Ladder - LeetCode 注意点 每一个变化的字母都要在wordList中 解法 解法一:bfs.类似走迷宫,有26个方向(即26个字 ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Word Ladder——LeetCode
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- Word Search leetcode java
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- Word Break leetcode java
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- Xamarin iOS教程之视图显示图像
Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...
- List,Set的区别
1.List,Set都是继承自Collection接口2.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该 ...
- P4810 A’s problem(a)
P4810 A’s problem(a)From: admin 时间: 1000ms / 空间: 65536KiB / Java类名: Main 背景 清北NOIP春季系列课程 描述 这是一道有背景的 ...
- mongodb用mongoose查库的对象,不能增加属性
node + koa2 + mongodb 写了一个给前端的接口 如果不是写这个接口,这辈子都发现不了mongodb里这个大坑 mongoose 是个ODM(Object Document Mappe ...
- MikroTik RouterOS安装方法收集(转)
注意:ROS无法使用U盘安装,如果要使用ISO文件进行安装就必须使用光驱以及刻录成光盘,并且只能使用IDE的光驱. 一.首先了解常见的ROS硬件架构: X86架构(也是最常用的) mipsbe(欧米t ...
- 通过Windows Compatibility Pack补充.net core中缺失的api
把项目往.net core上迁移的时候,一个最大的问题就是和.net framework相比,有一部分api缺失.它主要分为两类: Windows 独有的api,如注册表 未完成的功能,如System ...
- How to detect the types of executable files
How to detect the types of executable files type { IMAGE_DOS_HEADER: DOS .EXE header. } IMAGE_DOS_HE ...
- [Go] 反射 - reflect.ValueOf()
类型 和 接口 由于反射是基于类型系统(type system)的,所以先简单了解一下类型系统. 首先 Golang 是一种静态类型的语言,在编译时每一个变量都有一个类型对应,例如:int, floa ...
- cocos2d-x学习资源汇总
http://blog.csdn.net/akof1314 http://blog.csdn.net/bill_man/ http://blog.csdn.net/fylz1125/ MoonWa ...
- Windows XP Manifest in Delphi
Find out how you can include the manifest into a Delphi project to allow your application to share t ...