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的更多相关文章

  1. Leetcode 126.单词接龙II

    单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...

  2. [LeetCode] 126. 单词接龙 II

    题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...

  3. Java实现 LeetCode 127 单词接龙

    127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...

  4. 126. 单词接龙 II

    题目: 链接:https://leetcode-cn.com/problems/word-ladder-ii/ 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出 ...

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

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

  6. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  7. 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-> ...

  8. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  9. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

随机推荐

  1. [poj2778 DNA Sequence]AC自动机,矩阵快速幂

    题意:给一些字符串的集合S和整数n,求满足 长度为n 只含charset = {'A'.'T‘.'G'.'C'}包含的字符 不包含S中任一字符串 的字符串的种类数. 思路:首先对S建立ac自动机,考虑 ...

  2. [hdu2594]kmp水题

    题意:求最长的a的前缀同时满足是b的后缀,把a,b连在一起,kmp跑一下,迭代next直到长度小于等于a,b长度的最小值为止,即为答案. #pragma comment(linker, "/ ...

  3. windows电脑关闭自动更新的方法

    第一步.打开我的电脑,点击此电脑,然后点击管理 第二步.在计算机管理(本地)里面找到服务和应用程序,点击进入 第三步.进去,点击服务 第四步.往下滑,找到windows update,点击进入 第五步 ...

  4. 这么简单的ES索引生命周期管理,不了解一下吗~

    对于日志或指标(metric)类时序性强的ES索引,因为数据量大,并且写入和查询大多都是近期时间内的数据.我们可以采用hot-warm-cold架构将索引数据切分成hot/warm/cold的索引.h ...

  5. 计算python内部数据结构时间效率-源代码

    #Author:qinjiaxi '''本程序计算各种循环的时间效率''' from timeit import Timer def test1(n): li = [] for i in range( ...

  6. 弹性式数据集RDD

    弹性式数据集RDD ​ 一.RDD简介二.创建RDD        2.1 由现有集合创建        2.2 引用外部存储系统中的数据集        2.3 textFile & who ...

  7. React实践debug:JSX输出的限制(存疑)

    今天在练习React构建组件的时候遇到一个问题. 由于文档中反复提倡将组件尽可能按功能单位分解复用.在练习用React做一个todolist时候,我把todolist分解成两部分: class Tod ...

  8. linux -- 一般使用经验(四)

    一.使用grep进行条件筛选(主要为日志) 1.grep  -E '条件1|条件2|条件3'   文件名 (cat -n 文件.log |grep -E '2020-01-16 15:24:48|条件 ...

  9. SQL——SELECT、UPDATE、DELETE和INSERT INTO

    SQL是一种ANSI的标准计算机语言.ANSI:美国国家标准化组织.除SQL标准外,大部分SQL数据库都拥有私有的扩展.SQL对大小写不敏感.某些数据库系统要求在SQL命令末端使用分号,这样可以执行一 ...

  10. iOS 的尾调用优化原理

    背景: 今天聊代码规范的问题的时候说了一下尾调用的问题. 一:概念: 什么是尾调用? 尾调用(Tail Call):某个函数的最后一步仅仅只是调用了一个函数(可以是自身,可以是另一个函数). 注意 “ ...