Java实现 LeetCode 126 单词接龙 II
126. 单词接龙 II
给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:
每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:
如果不存在这样的转换序列,返回一个空列表。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
输出:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
示例 2:
输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
输出: []
解释: endWord “cog” 不在字典中,所以不存在符合要求的转换序列。
class Solution {
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
//结果
List<List<String>> res = new ArrayList<>();
if(wordList == null) return res;
//bfs搜索所用的字典
Set<String> dicts = new HashSet<>(wordList);
if(!dicts.contains(endWord)) return res;
if(dicts.contains(beginWord)) dicts.remove(beginWord);
//bfs搜索最短路径所用的开始和结束的字典
Set<String> endList = new HashSet<>(),
beginList = new HashSet<>();
//每个点所对应的邻接点,list
Map<String, List<String>> map = new HashMap<>();
beginList.add(beginWord);
endList.add(endWord);
bfs(map, beginList, endList, beginWord, endWord,dicts, false);
//dfs的前进路线保存list
List<String> subList = new ArrayList<>();
subList.add(beginWord);
dfs(map, res, subList, beginWord, endWord);
return res;
}
void dfs (Map<String, List<String>> map,
List<List<String>> result, List<String> subList,
String beginWord, String endWord) {
if(beginWord.equals(endWord)) {
result.add(new ArrayList<>(subList));
return;
}
if (!map.containsKey(beginWord)) {
return;
}
for (String word : map.get(beginWord)) {
subList.add(word);
dfs(map, result, subList, word, endWord);
subList.remove(subList.size() - 1);
}
}
//reverse是双端bfs的一个优化
void bfs(Map<String, List<String>> map, Set<String> beginList, Set<String> endList, String beginWord, String endWord,Set<String> wordList, boolean reverse){
if(beginList.size() == 0) return;
wordList.removeAll(beginList);
boolean finish = false;
Set<String> temp = new HashSet<>();
for(String str : beginList){
char[] charr = str.toCharArray();
for(int chI = 0; chI < charr.length; chI++){
char old = charr[chI];
for(char ch = 'a'; ch <= 'z'; ch++){
if(ch == old)
continue;
charr[chI] = ch;
String newstr = new String(charr);
if(!wordList.contains(newstr)){
continue;
}
//若是在某一层找到了最后的节点,就直接标记找到了,即一票决定。这里因为要找所有的最短路径,所以循环还是要继续的。
if(endList.contains(newstr)){
finish = true;
}else{
temp.add(newstr);
}
//无论怎么变换方向,永远用开始方向的字符做key,是为了后面的dfs,单一方向搜索
String key = reverse? newstr:str;
String value = reverse ? str : newstr;
if(!map.containsKey(key)){
map.put(key, new ArrayList<>());
}
map.get(key).add(value);
}
charr[chI] = old;
}
}
if(!finish) {
if(temp.size() > endList.size()){
bfs(map, endList, temp, beginWord, endWord,wordList, !reverse);
}else{
bfs(map, temp, endList, beginWord, endWord, wordList, reverse);
}
}
}
}
Java实现 LeetCode 126 单词接龙 II的更多相关文章
- Leetcode 126.单词接龙II
单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...
- [LeetCode] 126. 单词接龙 II
题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...
- Java实现 LeetCode 127 单词接龙
127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...
- 126. 单词接龙 II
题目: 链接:https://leetcode-cn.com/problems/word-ladder-ii/ 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出 ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Java for LeetCode 092 Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- Java实现 LeetCode 212 单词搜索 II(二)
212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
随机推荐
- [csu/coj 1080]划分树求区间前k大数和
题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...
- 帝国cms列表页内容简介字段smalltext去除里面html格式代码 设置方法
帝国cms列表页内容简介字段smalltext去除里面html格式代码帝国cms列表页调用内容简介出现html代码怎么办 近来在用帝国cms的时候,发现一个问题,在列表页调用产品简介的时候出现了这种h ...
- activiti工作流入门学习
工作流一般在OA系统用的比较多,当然,只要有流程审批的地方都会用到,activiti只是开源的工作流中比较流行的一个,还有其他的开源的工作流,这里学习activiti工作流:前面部分是关于activi ...
- layui select下拉菜单联动
做的比较简单,先从后台直接把第一级菜单输出,然后点击二级菜单的时候再动态展示 <div class="layui-inline"> <label class=&q ...
- Redis学习笔记(八) RDB持久化
Redis是内存数据库,它将自己的数据库状态存储在内存里面,所以如果不想办法将存储在内存中的数据库状态保存到磁盘,那么服务器 进程一旦退出,服务器中的数据库状态也会消失不见. 为了解决这个问题,Red ...
- Spring全家桶之springMVC(一)
Spring MVC简介和第一个spring MVC程序 Spring MVC是目前企业中使用较多的一个MVC框架,被很多业内人士认为是一个教科书级别的MVC表现层框架,Spring MVC是大名鼎鼎 ...
- 如何理解golang中的nil
nil的奇怪行为 刚接触golang时,发现nil在不同的上下文,行为表现是不同的,并且和其他语言中的表现,也不大相同 实例1:输入true, true, false,不符合传递性 func main ...
- 「雕爷学编程」Arduino动手做(32)——雨滴传感器模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- node mysql模块写入中文字符时的乱码问题
刚刚发现一个奇怪的问题: 在node上用mysql模块将数据写入数据库的时候,在cmd上打开mysql发现select出来的中文是乱码.但这就奇怪了. 因为本机在安装mysql的时候就已经在配置文件将 ...
- 在php文件中xml格式
本人是小白,有错误的地方请指正,勿喷! 在写一个调查问卷的过程中用到了xml文件,如想要了解,可以通过以下链接简单学习:http://www.w3school.com.cn/xml/ 所用工具:php ...