LeetCode007:WordLadder
来源:互联网 发布:apache高并发解决方案 编辑:程序博客网 时间:2024/05/22 03:26
参考:http://www.cnblogs.com/springfor/p/3893499.html
题目:
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 dot lot dict = [dog log]
aot bot ... yot zot,dat dbt ...dyt dzt,doa dob ... dog .. doy doz,
aot bot ... yot zot,lat lbt ... lyt lzt,loa lob ... log... loy loz
level = 4 dog log dict = []
aog bog cog
level = 5 cog dict = []
package com.abuge;import java.util.LinkedList;import java.util.Set;import java.util.TreeSet;import org.junit.Test;/** * 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 timeEach intermediate word must exist in the dictionaryFor 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 * @author AbuGe * */public class Solution {@Testpublic void test(){String start = "hit";String end = "cog";Set<String> dict = new TreeSet<String>();dict.add("hot");dict.add("dot");dict.add("dog");dict.add("lot");dict.add("log");System.out.println(new Solution().laddderLength(start, end, dict));}/** * 思路:(没递归进一层,就去除字典中的相关词,从而避免重复,对于同一层的候选词,谁先到达目的单词谁先返回,则得到最短路径) * 1、使用LinkedList队列维护符合要求的字符串 * 2、通过BFS搜索符合要求的路径 * @param start * @param end * @param dict * @return */public int laddderLength(String start, String end, Set<String> dict){//单词为空,或者长度不等或者长度为零的情况if(start == null || end == null || start.length() != end.length() || start.length() == 0 || end.length() == 0)return 0;//创建一个LinkedList维护单词队列LinkedList<String> wordQueue = new LinkedList<String>();//将起始单词加入队列wordQueue.add(start);//定义相关变量int len = 1;//记录路径长度int curNum = 1;//记录当前层次的单词数int nextNum = 0;//记录下一层的候选单词数//递归调用while(!wordQueue.isEmpty()){String word = wordQueue.poll();curNum--;for(int i = 0; i < word.length(); i++){char[] wordArray = word.toCharArray();for(char j = 'a'; j < 'z'; j++){wordArray[i] = j;String tmp = new String(wordArray);if(tmp.equals(end))return len + 1;if(dict.contains(tmp)){wordQueue.add(tmp);nextNum++;dict.remove(tmp);}}}if(curNum == 0){curNum = nextNum;nextNum = 0;len++;}}return 0; }}
- LeetCode007:WordLadder
- wordLadder
- WordLadder
- leetcode-WordLadder
- Leetcode127: wordLadder I
- 刷题——WordLadder
- leetcode007:Reverse Integer
- LeetCode007--Reverse Integer
- leetcode007 Reverse Integer
- leetcode007 Reverse Integer
- Leetcode007-Reverse Integer
- LeetCode007 Reverse Integer
- leetcode007-3Sum Closest
- Leetcode007 Reverse Integer
- Leetcode007--将字符串转换成整形
- Leetcode刷题系列(十八)CombinationSum && WordLadder
- 【LeetCode007算法/编程练习C++】数字逆序(atol)
- Hive - hive.groupby.skewindata环境变量与负载均衡
- EBS Multi Org 基础
- zoj 1028
- android得到系统时间如何判断是白天还是晚上
- android开发中如何查看当前eclipse 是否已经安装ADT以及这个ADT的版本
- LeetCode007:WordLadder
- Windows Azure Mobiles Services实现客户端的登录注册
- ODBC连接数据库
- Binary Tree Traversals
- UVa-Getting Started
- 韩顺平PHP学习视频笔记整理006浮动窗口 表单及表单控件①
- Hibernate查询方式汇总
- Spring事务传播性与隔离级别
- 天才吧是什么???