leetcode之word ladder
对于之前没有接触过该类型题目的人来说,此题无疑是个难题,本人提交了10次才正确通过,期间遇到了非常多的问题,感觉几乎把OJ的所有错误遍历了一遍,下面详细说说自己做该题的经验。
首先承认,我一开始并没有想到什么图模型,或者说是一点思路都没有。然后我就冥思苦想,首先想到了可以先构造一个二维矩阵,判断给定的词之间是否能两两一步到达,这一步可以通过两层循环加字符串的遍历完成,应该不难。获得这个矩阵之后,我居然还是没有想到图,只是在纸上写写画画感觉问题可以转化成在矩阵中搜索一条路径可以到达最后一行。路径搜索应该可以通过深搜完成,之后就开始按照这个思路写代码,结果越写越乱,遇到多次MLE。
多次尝试无果后,我感觉之前的思路不对,需要重整思路。然后又是写写画画,突然灵光一闪:搜索路径貌似可以用图表示!然后按照这个思路很快就想明白了整个流程:首先构造一个无向图用来表示单词之间的可达性,然后从表示起点的节点开始对整个图进行BFS遍历,直到找到表示end的节点。按照这个思路也很快写出代码,但是提交后又遇到新问题:不停地超时。此时说明算法已经正确,但是复杂度还是太高。不妨先看一下这时的代码:
public int ladderLength2(String start, String end, HashSet<String> dict) {
if (start == null || end == null || start.equals(end)
|| start.length() != end.length())
return 0; if (isOneWordDiff(start, end))
return 2; dict.add(start);
dict.add(end); String[] dicts = (String[]) dict.toArray(new String[0]); int size = dicts.length; // 表示图的列表数组
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>(); int s = 0, e = 0;
// 判断一个单词一步可以到达哪些单词
for (int i = 0; i < size; i++) {
if (start.equals(dicts[i]))
s = i;
if (end.equals(dicts[i]))
e = i; ArrayList<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < size; j++) {
if (isOneWordDiff(dicts[i], dicts[j]))
list.add(j);
} lists.add(list);
} HashMap<Integer,Integer> dist = new HashMap<Integer, Integer>(); Queue<Integer> queue = new LinkedList<Integer>();
queue.add(s);
dist.put(s, 1); while (!queue.isEmpty()) {
int index = queue.poll();
ArrayList<Integer> list = lists.get(index); for (int i = 0; i < list.size(); i++) {
if (list.get(i) == e) {
return dist.get(index) + 1;
} if (!dist.containsKey(list.get(i))) {
queue.add(list.get(i));
dist.put(list.get(i), dist.get(index) + 1);
}
}
} return 0;
}
private boolean isOneWordDiff(String a, String b) {
int diff = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i)) {
diff++;
if (diff >= 2)
return false;
}
} return diff == 1;
}
分析一下上面代码的思路:
1.首先将start和end添加到词典中,并将词典转化成数组;
2.创建一个二维数组,二维数组的每一维表示某个单词可以通过一步转化到达的单词,也即利用邻接表的方式存储图结构。在这个过程中也获得了start和end代表的数字;
3.利用构造的图进行BFS遍历,直到遇到end节点或者返回0。在遍历的过程中,由于图的边长是1,所以我们在遍历的时候总是得到从start到某个节点的最短路径,所以我们只需要考虑尚未遍历过的顶点即可。
上述代码是正确的,但是一直超时真是让人搞不清状况。后来,只能上网搜索别人的解答才明白其中的原因。超时的地方不在BFS遍历,而是在我们构造图的地方。我们采用了两层遍历构造一个图,此时复杂度是O(n2),数据量很小时可能体现不出它的劣势,但是当n上千时(给定的测试集中有这样的例子),上面的方法构造图就显得太慢。
这里我们可以不用实际构造图,而在BFS遍历的时候去寻找当前单词可达的下一个单词。如果还是通过遍历所有的单词判断是否可达,则复杂度和上面一样,但实际上在上千个单词中,只有少数几个可以由当前单词一步到达,我们之前的比较浪费了很多时间在不可能的单词上。网上对该问题的解决无一例外都是按照下面的思路:将当前单词每一个字符替换成a~z的任意一个字符,然后判断是否在词典中出现。此时的复杂度是O(26*word_length),当单词比较短时,这种方法的优势就体现出来了。按照这种思路修改后的代码如下:
public int ladderLength(String start, String end, HashSet<String> dict) {
if (start == null || end == null || start.equals(end)
|| start.length() != end.length())
return 0; if (isOneWordDiff(start, end))
return 2; Queue<String> queue=new LinkedList<String>();
HashMap<String,Integer> dist=new HashMap<String,Integer>(); queue.add(start);
dist.put(start, 1); while(!queue.isEmpty())
{
String head=queue.poll(); int headDist=dist.get(head);
//从每一个位置开始替换成a~z
for(int i=0;i<head.length();i++)
{
for(char j='a';j<'z';j++)
{
if(head.charAt(i)==j) continue; StringBuilder sb=new StringBuilder(head);
sb.setCharAt(i, j); if(sb.toString().equals(end)) return headDist+1; if(dict.contains(sb.toString())&&!dist.containsKey(sb.toString()))
{
queue.add(sb.toString());
dist.put(sb.toString(), headDist+1);
}
}
}
} return 0;
}
我们可以看出,代码更加简洁,而且效率也更高,提交上述代码就可以AC了。
leetcode之word ladder的更多相关文章
- 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
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 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 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 ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- 服务器&阵列卡&组raid 5
清除raid信息后,机器将会读不到系统, 后面若进一步操作处理, raid信息有可能会被初始化掉,那么硬盘数据就有可能会被清空, 导致数据丢失, 否则如果只是清除raid信息,重做raid是可以还原系 ...
- [转]关于OpenGL的绘制上下文
[转]关于OpenGL的绘制上下文 本文转自(http://www.cnblogs.com/Liuwq/p/5444641.html) 什么是绘制上下文(Rendering Context) 初学Op ...
- java中的final和volatile详解
相比synchronized,final和volatile也是经常使用的关键字,下面聊一聊这两个关键字的使用和实现 1.使用 final使用: 修饰类表示该类为终态类,无法被继承 修饰方法表示该方法无 ...
- xshell连接centos与ubuntu
操作系统:Windows 7 应用软件:Ware Workstation &Xshell 5 Linux:CentOS 7 Minimal &Ubuntu Server 16 ==== ...
- Docker容器访问控制
容器的访问控制,主要通过 Linux 上的 iptables 防火墙来进行管理和实现.iptables 是 Linux 上默认的防火墙软件,在大部分发行版中都自带. 容器访问外部网络 容器要想访问外部 ...
- Throughtput收集器
介绍 JVM里面的Throughtput收集器是一款关注吞吐量的垃圾收集器.该收集器是唯一一个实现了UseAdaptiveSizePolicy策略的收集器,允许用户通过指定最大暂停时间和垃圾收集时间占 ...
- CSAPP缓冲区溢出攻击实验(下)
CSAPP缓冲区溢出攻击实验(下) 3.3 Level 2: 爆竹 实验要求 这一个Level的难度陡然提升,我们要让getbuf()返回到bang()而非test(),并且在执行bang()之前将g ...
- Scala:类,对象和特征(接口)
http://blog.csdn.net/pipisorry/article/details/52902609 Scala类和对象 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象 ...
- ubuntu垃圾清理命令
ubuntu的空间莫名不够用了 通过系统自带的工具磁盘使用分析器,发现var文件下面的log100多个g,这个日志文件是可以删除的,然后tmp文件也是可以删除的. 1.sudo rm -rf /tmp ...
- C算法实现:将字符串中的数字返回为整型数
今天看linux内核驱动的代码,发现一个算法写得挺简单,也有意思. 分享一下我的测试代码: #include <stdio.h> typedef int U32 ; U32 String2 ...