单词接龙II

给定两个单词(beginWordendWord)和一个字典 wordList,找出所有从 beginWord endWord 的最短转换序列。转换需遵循如下规则:

  1. 每次转换只能改变一个字母。
  2. 转换过程中的中间单词必须是字典中的单词。

说明:

  • 如果不存在这样的转换序列,返回一个空列表。
  • 所有单词具有相同的长度。
  • 所有单词只由小写字母组成。
  • 字典中不存在重复的单词。
  • 你可以假设 beginWordendWord 是非空的,且二者不相同。

示例 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" 不在字典中,所以不存在符合要求的转换序列。

先BFS遍历图,找到所有到达结尾单词的最短路径,同时记录图。最后再DFS遍历图。

BFS遍历图:queue用来BFS遍历图。队列按结点的深度依次存放待处理的结点。首先存放第一层结点,弹出,根据第一层结点找到所有第二层结点放入队列;弹出第二层某个结点,根据此结点找到所有第三层结点放入队列,以此类推。

记录图:记录图中每个结点的父节点们。记录图中结点的层数(深度)。

DFS遍历记录的图得出结果。

 class Solution{
public boolean isDiffOneWord(String a,String b){
int diffnum=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i)!=b.charAt(i)){
diffnum++;
}
if(diffnum==2){
return false;
}
}
if(diffnum==1){
return true;
}
return false;
} public List<List<String>> findLadders(String beginWord,String endWord,List<String> wordList){
List<List<String>> res=new ArrayList<List<String>>();
int endIndex=wordList.indexOf(endWord);
if(endIndex==-1){
return res;
}
int beginIndex=wordList.indexOf(beginWord);
//若beginWord不在wordList中,则添加至wordList末尾
if(beginIndex==-1){
wordList.add(beginWord);
beginIndex=wordList.size()-1;
}
//queue用来BFS遍历图。队列按节点的深度依次存放待处理的节点。首先存放第一层结点,弹出
//根据第一层结点找到所有第二层结点放入队列;以此类推
Queue<Integer> queue=new LinkedList<Integer>();
queue.offer(beginIndex);
//fatherNodes、height用来记录图
//记录图中每个节点的父亲节点们
Map<Integer,List<Integer>> fatherNodes=new HashMap<Integer,List<Integer>>();
for(int i=0;i<wordList.size();i++){
fatherNodes.put(i,new ArrayList<Integer>());
}
//记录图中节点的层数(深度)
int[] height=new int[wordList.size()];
height[beginIndex]=1;
while(!queue.isEmpty()){
int nowIndex=queue.poll();
//若最短深度的路径已经记录完毕,退出循环
//height[nowIndex]>=height[endIndex]针对多个父亲点的情况
if(height[endIndex]!=0 && height[nowIndex]>=height[endIndex]){
break;
}
for(int i=0;i<wordList.size();i++){
//height[i]==0未访问过的节点,height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
if((height[i]==0 || height[i]==height[nowIndex]+1) && isDiffOneWord(wordList.get(nowIndex),wordList.get(i))){
if(height[i]==0){
queue.offer(i);
height[i]=height[nowIndex]+1;
fatherNodes.get(i).add(nowIndex);
}else{
//height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
fatherNodes.get(i).add(nowIndex);
}
}
}
} if(height[endIndex]==0){
return res;
}else{
List<String> list=new ArrayList<String>();
list.add(wordList.get(endIndex));
dfs(endIndex,list,res,wordList,fatherNodes,beginIndex);
}
return res;
} public void dfs(int lastIndex,List<String> list,List<List<String>> res,List<String> wordList,Map<Integer,List<Integer>> fatherNodes,int beginIndex){
if(lastIndex==beginIndex){
List<String> newList=new ArrayList<String>(list);
Collections.reverse(newList);
res.add(newList);
return;
}
for(int i=0;i<fatherNodes.get(lastIndex).size();i++){
int fatherIndex=fatherNodes.get(lastIndex).get(i);
list.add(wordList.get(fatherIndex));
dfs(fatherIndex,list,res,wordList,fatherNodes,beginIndex);
list.remove(list.size()-1);
}
}
}

Leetcode 126.单词接龙II的更多相关文章

  1. Java实现 LeetCode 126 单词接龙 II

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

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

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

  3. 126. 单词接龙 II

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

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

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

  5. Java实现 LeetCode 127 单词接龙

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

  6. [Swift]LeetCode126. 单词接龙 II | Word Ladder II

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

  7. lintcode 单词接龙II

    题意 给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列 比如: 1.每次只能改变一个字母. 2.变换过程中的中间单词必须在字典中出现. 注意事项 所有单词具有相 ...

  8. leetcode 127 单词接龙

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

  9. leetcode 137单词接龙

    直接层序遍历,结果有部分测试样例超时: class Solution { public: int ladderLength(string beginWord, string endWord, vect ...

随机推荐

  1. 基于Linux系统的Shell编程-基础篇

    1. Shell基础介绍 1.1 Shell编程的意义 为什么使用shell编程 节约时间 1.2 显示脚本执行过程 前面有+表示执行过的命令的 前面没有东西,表示输出到屏幕上的内容. [root@C ...

  2. 【废弃中】【WIP】JavaScript 数组

    创建: 2018/01/22 更新: 2018/05/20 把此博文加入[javascript]分类, 原来忘记了 废弃: 2019/02/19 重构此篇.原文归入废弃  增加[废弃中]标签与总体任务 ...

  3. Python基础 — Pandas

    Pandas -- 简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.        Pandas ...

  4. 【BZOJ2525】[Poi2011]Dynamite(二分,树形dp)

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

  5. JavaScript--DOM删除节点removeChild()

    删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点.如删除成功,此方法可返回被删除的节点,如失败,则返回 NULL. 语法: nodeObject.remo ...

  6. 贪心 CodeForces 137B Permutation

    题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...

  7. java IO流 之 字节输出流 OutputString()

    Java学习重点之一:OutputStream 字节输出流的使用 FileOutPutStream:子类,写出数据的通道 步骤: 1.获取目标文件 2.创建通道(如果原来没有目标文件,则会自动创建一个 ...

  8. jQuery四叶草菜单效果,跟360杀毒软件差不多

    首先,我们要在js,css文件夹中创建js跟css,然后在body中写入html代码 <main><!--标签是 HTML 5 中的新标签. 素中的内容对于文档来说应当是唯一的.它不 ...

  9. JDBC更新10W级以上数据性能优化

    随笔缘由: 系统完成到一定程度,少不了要往数据库中添加大量数据进行性能测试. 我用程序做数据10W条,使用jdbc批更新的API,发现每次只能插入2W多条记录. 一番小小研究,觉得总结一下可能有些意义 ...

  10. 更新html技术比较

    document.write() document对象的write方法可以很简单的向页面的源代码中添加内容,不过不推荐使用. 优点:可以快速简单的让初学者理解如何向页面添加内容: 缺点: 只有页面初始 ...