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,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
随机推荐
- 阿里面试居然跟我扯了半小时的CyclicBarrier
一个大腹便便,穿着格子衬衫的中年男子,拿着一个贴满Logo的Mac向我走来,看着稀少的头发,我心想着肯定是顶级技术大牛吧!但是我也是一个才华横溢的人,稳住我们能赢. 面试官:您好,先做一下自我介绍吧! ...
- 01背包问题,dp和贪心解法(c++11)
dp解法: 令dp[i]表示容量为i的背包所能得到的最大价值,考虑在当前物品集合中加入1个新考虑的物品i,则有如下状态转移方程:dp[j] = max(dp[j], dp[j - weight[i]] ...
- [CodeForces 344D Alternating Current]栈
题意:两根导线绕在一起,问能不能拉成两条平行线,只能向两端拉不能绕 思路:从左至右,对+-号分别进行配对,遇到连续的两个“+”或连续的两个“-”即可消掉,最后如果全部能消掉则能拉成平行线.拿两根线绕一 ...
- Python 接口自动化测试
1. 接口基础知识 1.1 接口分类 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口. (1) webservice接口:走soap协议通过http传输,请求报文和返回报文都是xml格 ...
- Redis学习笔记(九) AOF持久化
除了RDB持久化功能之外,Redis还提供了AOF持久化功能.与RDB持久化通过保存数据库中的键值对来记录数据库状态不同,AOF持久化是通过保存Redis服务器所执行的写命令来记录数据库状态的. 服务 ...
- webpack从零的实践(新手良药)
1. 什么是webpack? 本质上,webpack是一个现代javascript应用程序的静态模块打包器.webpack处理应用程序时,它会递归地构建一个依赖关系图(dependency graph ...
- babel转码时generator的regeneratorRuntime
今天写generator函数时发现出错:regeneratorRuntime. 在stackoverflow网友说需是本地babel软件包没有安装完全. package.json: "dev ...
- linux常用命令---计划定时任务
计划定时任务(crontab) 存放定时任务的文件 /var/spool/cron systemctl status cron ps -ef|grep crond 检测crontab是否开机启动 sy ...
- 51Nod栈
LYK有一个栈,众所周知的是这个数据结构的特性是后进先出的. LYK感觉这样子不太美妙,于是它决定在这个前提下将其改进,也就是说,每次插入元素时,可以在栈顶或者栈底插入,删除元素时,只能在栈顶删除.L ...
- 你以为只有马云会灌鸡汤?Linux 命令行也会!
你以为只有马云会灌鸡汤?Linux 命令行也会! "Linux 太南了o(╥﹏╥)o","我累了不想奋斗了o(︶︿︶)o"... 不知道你有没有想过,在你快丧失 ...