单词接龙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遍历记录的图得出结果。

  1. class Solution{
  2. public boolean isDiffOneWord(String a,String b){
  3. int diffnum=0;
  4. for(int i=0;i<a.length();i++){
  5. if(a.charAt(i)!=b.charAt(i)){
  6. diffnum++;
  7. }
  8. if(diffnum==2){
  9. return false;
  10. }
  11. }
  12. if(diffnum==1){
  13. return true;
  14. }
  15. return false;
  16. }
  17.  
  18. public List<List<String>> findLadders(String beginWord,String endWord,List<String> wordList){
  19. List<List<String>> res=new ArrayList<List<String>>();
  20. int endIndex=wordList.indexOf(endWord);
  21. if(endIndex==-1){
  22. return res;
  23. }
  24. int beginIndex=wordList.indexOf(beginWord);
  25. //若beginWord不在wordList中,则添加至wordList末尾
  26. if(beginIndex==-1){
  27. wordList.add(beginWord);
  28. beginIndex=wordList.size()-1;
  29. }
  30. //queue用来BFS遍历图。队列按节点的深度依次存放待处理的节点。首先存放第一层结点,弹出
  31. //根据第一层结点找到所有第二层结点放入队列;以此类推
  32. Queue<Integer> queue=new LinkedList<Integer>();
  33. queue.offer(beginIndex);
  34. //fatherNodes、height用来记录图
  35. //记录图中每个节点的父亲节点们
  36. Map<Integer,List<Integer>> fatherNodes=new HashMap<Integer,List<Integer>>();
  37. for(int i=0;i<wordList.size();i++){
  38. fatherNodes.put(i,new ArrayList<Integer>());
  39. }
  40. //记录图中节点的层数(深度)
  41. int[] height=new int[wordList.size()];
  42. height[beginIndex]=1;
  43. while(!queue.isEmpty()){
  44. int nowIndex=queue.poll();
  45. //若最短深度的路径已经记录完毕,退出循环
  46. //height[nowIndex]>=height[endIndex]针对多个父亲点的情况
  47. if(height[endIndex]!=0 && height[nowIndex]>=height[endIndex]){
  48. break;
  49. }
  50. for(int i=0;i<wordList.size();i++){
  51. //height[i]==0未访问过的节点,height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
  52. if((height[i]==0 || height[i]==height[nowIndex]+1) && isDiffOneWord(wordList.get(nowIndex),wordList.get(i))){
  53. if(height[i]==0){
  54. queue.offer(i);
  55. height[i]=height[nowIndex]+1;
  56. fatherNodes.get(i).add(nowIndex);
  57. }else{
  58. //height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
  59. fatherNodes.get(i).add(nowIndex);
  60. }
  61. }
  62. }
  63. }
  64.  
  65. if(height[endIndex]==0){
  66. return res;
  67. }else{
  68. List<String> list=new ArrayList<String>();
  69. list.add(wordList.get(endIndex));
  70. dfs(endIndex,list,res,wordList,fatherNodes,beginIndex);
  71. }
  72. return res;
  73. }
  74.  
  75. public void dfs(int lastIndex,List<String> list,List<List<String>> res,List<String> wordList,Map<Integer,List<Integer>> fatherNodes,int beginIndex){
  76. if(lastIndex==beginIndex){
  77. List<String> newList=new ArrayList<String>(list);
  78. Collections.reverse(newList);
  79. res.add(newList);
  80. return;
  81. }
  82. for(int i=0;i<fatherNodes.get(lastIndex).size();i++){
  83. int fatherIndex=fatherNodes.get(lastIndex).get(i);
  84. list.add(wordList.get(fatherIndex));
  85. dfs(fatherIndex,list,res,wordList,fatherNodes,beginIndex);
  86. list.remove(list.size()-1);
  87. }
  88. }
  89. }

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. bzoj 1628: [Usaco2007 Demo]City skyline【贪心+单调栈】

    还以为是dp呢 首先默认答案是n 对于一个影子,如果前边的影子比它高则可以归进前面的影子,高处的一段单算: 和他一样高的话就不用单算了,ans--: 否则入栈 #include<iostream ...

  2. SQL Server触发器创建、删除、修改、查看示例步骤

    一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约`束. 二﹕ SQL Server为每个触发 ...

  3. java entity

    对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段.好处:1.对对象实体的封装,体现OO思想.2.属性可以对字段 ...

  4. [ZJOI2006]GameZ游戏排名系统

    Description GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在世界上的排名.得分越高,排名就越靠前.当两个玩家的名次相同时 ...

  5. JS数组、数组和学生对象

    <html> <head> <meta charset="utf-8"> <title>JS</title> </ ...

  6. Visual Studio 相关

    基础配置: 背景色:豆沙绿(色调84 饱和度118 亮度205) 字体字号:Consolas 11号  离线下载方法: vs_enterprise.exe --layout c:\vs2017offl ...

  7. 生成清除某个数据库下的所有表的SQL语句

    方法1:重建库和表 用mysqldump --no-data把建表SQL导出来,然后drop database再create database,执行一下导出的SQL文件: 方法2:生成清空所有表的SQ ...

  8. Java开发笔记(九十二)文件通道的基本用法

    前面介绍的各色流式IO在功能方面着实强大,处理文件的时候该具备的操作应有尽有,可流式IO在性能方面不尽如人意,它的设计原理使得实际运行效率偏低,为此从Java4开始增加了NIO技术,通过全新的架构体系 ...

  9. 【Android学习笔记】Mac下Android Studio开发环境搭建

    本文由@ray 出品,转载请注明出处.  文章链接:http://www.cnblogs.com/wolfray/p/7829069.html 对于移动端这块,笔者之前一直都是进行iOS开发的,也从来 ...

  10. esp8266 SOC方案经过半年沉淀之后再度重启二

    2018-08-2014:16:10 以下是输出控制 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);      GPIO_OUTPUT_SET ...