Java for LeetCode 126 Word Ladder II 【HARD】
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】的更多相关文章
- 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] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Leetcode#126 Word Ladder II
原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...
- leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
随机推荐
- navicat链接lunix平台上的数据库
xsell 4.navicat软件 想在链接数据库的得常规设置里设置: 链接名称.主机名(链接lunix平台后才干ping 通的ip地址) port.username.password 然后选择ssh ...
- 【性能优化】——前端性能优化之DOM
前言:本文参考学习自 RenChao Guan的博客,来源FSUX.ME,感谢原作者,本文的思维导图为自己整理 补充: 浏览器工作流程 避免重绘和回流的四种方式的具体实现
- 2017.2.9 开涛shiro教程-第十章-会话管理(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十章 会话管理(一) 10.1 会话 shiro提供的会话可以用 ...
- Mongodb性能调优
摘要 1. Mongodb 适用场景简介 2. Mongodb 性能监控与分析 3. Mongodb 性能优化建议 关于Mongodb的几个大事件 1.根据美国数据库知识大全官网发布的DB热度排行,M ...
- IP反查网站,ip反查接口,旁站查询接口大全,通过IP查域名汇总:
http://cn.bing.com/search?q=ip%3A220.181.111.85 http://dns.aizhan.com/?q=www.baidu.com http: ...
- Vue2.0 引用 exif.js 实现调用摄像头进行拍照功能以及图片上传功能
vue组件代码 <template> <div> <div style="padding:20px;"> <div class=" ...
- TCP/IP详解 卷一(第七、八章 Ping、Traceroute程序)
Ping程序 Ping程序由Mike Muuss编写,目的是为了测试另一台主机是否可达. 该程序发送一份ICMP回显请求报文给主机,并等待返回ICMP回显应答. ping程序还能测出到这台主机的往返时 ...
- Docker 三大核心工具
Docker-machineDocker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox. Digital Oc ...
- opencl教程
http://www.altera.com.cn/corporate/news_room/releases/2013/products/nr-opencl-sdk-13.0.html http://w ...
- 修改登陆织梦后台的“DedeCMS 提示信息”
修改方法: 在dedecms程序的include目录中找到文件common.func.php并对其进行编辑,把其中的“DedeCMS 提示信息”修改为自己想要的内容提示: 在dedecms程序的默认管 ...