【LeetCode】Word Ladder
2013-12-31 14:21
471 查看
Word Ladder
Total Accepted: 3388 Total Submissions: 21998 My 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.
好吧,我承认我看了很久才明白这题是什么意思。
描述一下,从start开始,每次变化一个字母,变化后的字母必须在dict中,问最短需要几步可以走到end。
BFS吧。
测试用例有bug,题目给出的例子没有当做test case。
给出的用例应该是end都在dict中。
所以以下的代码也可以AC,其实是有问题的。
Java AC
Total Accepted: 3388 Total Submissions: 21998 My 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.
好吧,我承认我看了很久才明白这题是什么意思。
描述一下,从start开始,每次变化一个字母,变化后的字母必须在dict中,问最短需要几步可以走到end。
BFS吧。
测试用例有bug,题目给出的例子没有当做test case。
给出的用例应该是end都在dict中。
所以以下的代码也可以AC,其实是有问题的。
public class Solution { public static int ladderLength(String start, String end,HashSet<String> dict) { if (dict == null || dict.isEmpty()) { return 0; } Queue<Node> strQueue = new LinkedList<Node>(); HashSet<String> visitedSet = new HashSet<String>(); strQueue.add(new Node(start, 0)); visitedSet.add(start); int wordLen = start.length(); while (!strQueue.isEmpty()) { Node node = strQueue.peek(); strQueue.poll(); for (int i = 0; i < wordLen; i++) { StringBuffer sb = new StringBuffer(node.word); for (char j = 'a'; j <= 'z'; j++) { sb.setCharAt(i, j); String tempWord = sb.toString(); if (dict.contains(tempWord) && !visitedSet.contains(tempWord)) { if (tempWord.equals(end)) { return node.len+2; }else { strQueue.add(new Node(tempWord, node.len+1)); visitedSet.add(tempWord); } } } } } return 0; } private static class Node{ String word; int len; public Node(String word, int len) { super(); this.word = word; this.len = len; } } }
Java AC
public class Solution { public int ladderLength(String start, String end,HashSet<String> dict) { if (dict == null || dict.isEmpty()) { return 0; } Queue<Node> strQueue = new LinkedList<Node>(); HashSet<String> visitedSet = new HashSet<String>(); strQueue.add(new Node(start, 1)); visitedSet.add(start); int wordLen = start.length(); while (!strQueue.isEmpty()) { Node node = strQueue.peek(); strQueue.poll(); for (int i = 0; i < wordLen; i++) { StringBuffer sb = new StringBuffer(node.word); for (char j = 'a'; j <= 'z'; j++) { sb.setCharAt(i, j); String tempWord = sb.toString(); if (tempWord.equals(end)) { return node.len+1; }else { if (dict.contains(tempWord) && !visitedSet.contains(tempWord)) { strQueue.add(new Node(tempWord, node.len+1)); visitedSet.add(tempWord); } } } } } return 0; } private static class Node{ String word; int len; public Node(String word, int len) { super(); this.word = word; this.len = len; } } }
相关文章推荐
- C 03 关键字:const extren if等
- protobuf
- 利用OpenCV实现图像边缘直方图输出
- 主机字节序和网络字节序
- linux下JDK环境变量的几种常用配置
- localhost 与 127.0.0.1 的区别
- 图文详解DevExpress Reports创建数据识别报表
- 总结整理时下流行的浏览器User-Agent大全
- 设置网络打印机并设置密码
- linux 两步搞定 Tomcat 下启用 https:// 访问
- TextField中的属性:text 与 htmlText
- android webview 去掉多点触控 去掉放大缩小
- 政府交通部门如何实现Windows网络的访问控
- Install-Package 那点事儿
- mongodb mapreduce示例
- 利用Squid反向代理搭建CDN缓存服务器加快Web访问速度
- Android动画学习笔记-Android Animation
- LoadRunner:工作路径问题
- Android访问网络,使用HttpURLConnection还是HttpClient?
- 创建我们的第一个servlet小程序