Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]

Return

[
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]
解题思路:

本题应该是目前遇到的最Hard的一道了,思路先按照Java for LeetCode 127 Word Ladder的代码进行dfs找到 ladderLength 然后以 ladderLength 为步长进行DFS,这里进行DFS需要从后往前(因为disMap是从前往后建立的,从前往后的话前期肯定有无数匹配,从后往前的话,只要匹配就是要找到alist)

JAVA实现如下:

static public List<List<String>> findLadders(String start, String end,Set<String> dict) {
List<List<String>> result = new LinkedList<List<String>>();
LinkedList<String> queue = new LinkedList<String>();
HashMap<String, Integer> disMap = new HashMap<String, Integer>();
queue.add(start);
disMap.put(start, 1);
int depth = -1;
findDepth: while (!queue.isEmpty()) {
String word = queue.poll();
for (int i = 0; i < word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
for (char ch = 'a'; ch <= 'z'; ch++) {
sb.setCharAt(i, ch);
String nextWord = sb.toString();
if (nextWord.equals(end)) {
depth = disMap.get(word) + 1;
break findDepth;
}
if (dict.contains(nextWord)
&& !disMap.containsKey(nextWord)) {
queue.add(nextWord);
disMap.put(nextWord, disMap.get(word) + 1);
}
}
}
}
if (depth > 0)
dfs(result, start, end, disMap, depth,new LinkedList<String>());
return result;
} static void dfs(List<List<String>> result, String start, String end,HashMap<String, Integer> disMap, int depth,List<String> alist) {
alist.add(0, end);
if (end.equals(start))
result.add(new LinkedList<String>(alist));
if (depth <= 1)
return;
String word = alist.get(0);
for (int i = 0; i < word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
for (char ch = 'a'; ch <= 'z'; ch++) {
sb.setCharAt(i, ch);
String nextWord = sb.toString();
if (disMap.containsKey(nextWord)&&disMap.get(nextWord)==depth-1) {
dfs(result, start, nextWord, disMap, depth - 1,alist);
alist.remove(0);
}
}
}
}

Java for LeetCode 126 Word Ladder II 【HARD】的更多相关文章

  1. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  2. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  3. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  4. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  5. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  6. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

    https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...

  7. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  8. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  9. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

随机推荐

  1. JSONModel解析Dictionary To Model /JSON To Model

    你在把字典转成object的时候还在按下面这样: self.id = [jsonDict objectForKey:@"id"]; self.name = [jsonDict ob ...

  2. GLSL 基础量定义 【转】

    转载:http://blog.csdn.net/misol/article/details/7658949   GLSL语法跟C语言非常相似: 1.数据类型: GLSL包含下面几种简单的数据类型 fl ...

  3. 优化算法——拟牛顿法之L-BFGS算法

    一.BFGS算法 在"优化算法--拟牛顿法之BFGS算法"中,我们得到了BFGS算法的校正公式: 利用Sherman-Morrison公式可对上式进行变换,得到 令,则得到: 二. ...

  4. 转: Linux下使用java -jar运行可执行jar包的正确方式

    from:  http://codepub.cn/2016/05/11/The-correct-way-to-use-java-jar-run-an-executable-jar-package-un ...

  5. java8新特性学习笔记(二) 使用流(各种API)

    筛选和切片 用谓词筛选,筛选出各个不相同的元素,忽略流中的头几个元素,或将流截断至指定长度 用谓词筛选 Stream接口支持filter方法,该操作接受一个谓词(返回一个boolean的函数) 作为参 ...

  6. C 共用体

    C 共用体 共用体是一种特殊的数据类型,允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定 ...

  7. python 查询,子查询以及1对多查询

    1.添加数据: # 方法1:对象.save() book = Book(**kwargs) book.save() # 方法2:类.create(**kwargs) Book.create(**kwa ...

  8. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted:  ...

  9. 在Centos 7上安装配置 Apche Kafka 分布式消息系统集群

    Apache Kafka是一种颇受欢迎的分布式消息代理系统,旨在有效地处理大量的实时数据.Kafka集群不仅具有高度可扩展性和容错性,而且与其他消息代理(如ActiveMQ和RabbitMQ)相比,还 ...

  10. rtems 4.11 时钟驱动(arm, beagle)

    根据bsp_howto手册,时钟驱动的框架主要在 c/src/lib/libbsp/shared/Clockdrv_shell.h 文件中实现 时钟初始化 时钟驱动初始化函数为 Clock_initi ...