方法一:(广度优先搜索算法)

class Solution {public int ladderLength(String beginWord, String endWord, List<String> wordList) {int L=beginWord.length();Map<String, List<String>> allComboDict=new HashMap<String, List<String>>();wordList.forEach(word->{for(int i=0;i<L;i++) {String new_Word=word.substring(0, i)+"*"+word.substring(i+1,L);List<String> transmations=allComboDict.getOrDefault(new_Word, new ArrayList<String>());transmations.add(word);allComboDict.put(new_Word, transmations);}});// BFS需要的队列Queue<Pair<String,Integer>> Q=new LinkedList<Pair<String,Integer>>();Q.add(new Pair<String, Integer>(beginWord, 1));// 记录是否访问过的集合Map<String, Boolean> visited=new HashMap<String, Boolean>();visited.put(beginWord, true);while(!Q.isEmpty()) {Pair<String, Integer> node=Q.remove();String word=node.getKey();int level=node.getValue();for(int i=0;i<L;i++) {String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);for(String adjacentWord : allComboDict.getOrDefault(new_word, new ArrayList<String>())) {if(adjacentWord.equals(endWord)) {return level+1;}if(!visited.containsKey(adjacentWord)) {visited.put(adjacentWord, true);Q.add(new Pair<String, Integer>(adjacentWord, level+1));}}}}return 0;}
}

方法二:(双向广度优先算法)

class Solution {public static int visitWordNode(Queue<Pair<String, Integer>> Q,Map<String, Integer> visited,Map<String, Integer> othersVisited,int L,Map<String, List<String>> allComboDict) {Pair<String, Integer> node=Q.remove();String word=node.getKey();int level=node.getValue();for(int i=0;i<L;i++) {String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);for(String adjacentWord : allComboDict.getOrDefault(new_word, new ArrayList<String>())) {if(othersVisited.containsKey(adjacentWord)) {return level+othersVisited.get(adjacentWord);}if(!visited.containsKey(adjacentWord)) {visited.put(adjacentWord, level+1);Q.add(new Pair<String, Integer>(adjacentWord, level+1));}}}return -1;}public int ladderLength(String beginWord, String endWord, List<String> wordList) {if(!wordList.contains(endWord)) {return 0;}int L=beginWord.length();Map<String, List<String>> allComboDict=new HashMap<String, List<String>>();wordList.forEach(word->{for(int i=0;i<L;i++) {String new_word=word.substring(0,i)+"*"+word.substring(i+1,L);List<String> transmations=allComboDict.getOrDefault(new_word, new ArrayList<String>());transmations.add(word);allComboDict.put(new_word, transmations);}});// 双向BFS需要的队列Queue<Pair<String, Integer>> Q_begin=new LinkedList<Pair<String,Integer>>();Queue<Pair<String, Integer>> Q_end=new LinkedList<Pair<String,Integer>>();Q_begin.add(new Pair<String, Integer>(beginWord, 1));Q_end.add(new  Pair<String, Integer>(endWord, 1));// 记录遍历过的点Map<String, Integer> visited_begin=new HashMap<String, Integer>();Map<String, Integer> visited_end=new HashMap<String, Integer>();visited_begin.put(beginWord, 1);visited_end.put(endWord, 1);while(!Q_begin.isEmpty() && !Q_end.isEmpty()) {int ans=visitWordNode(Q_begin,visited_begin,visited_end,L,allComboDict);if(ans>-1) {return ans;}ans=visitWordNode(Q_end, visited_end, visited_begin, L, allComboDict);if(ans>-1) {return ans;}}return 0;}
}

力扣-10.26-127相关推荐

  1. 【每日力扣10】有效的数独

    一.题目 请你判断一个 9 x 9 的数独是否有效.只需要 根据以下规则 ,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9  ...

  2. 6.10力扣 10号出去逛街了,11号补上! 打印n位数

    557. 反转字符串中的单词 III class Solution:def reverseWords(self, s: str) -> str:arr=s.split(' ')res=[]for ...

  3. (C语言)力扣第26题,删除有序素组中的重复项

    暴力法: int removeDuplicates(int* nums, int numsSize) {for(int i=0;i<numsSize;)//遍历{if(nums[i]==nums ...

  4. 力扣(LeetCode)刷题,简单+中等题(第26期)

    目录 第1题:字典序排数 第2题:字符串解码 第3题:查找常用字符 第4题:所有奇数长度子数组的和 第5题:长按键入 第6题:分割字符串的最大得分 第7题:回文链表 第8题:有多少小于当前数字的数字 ...

  5. 力扣(LeetCode)刷题,简单题(第10期)

    目录 第1题:有序数组的平方 第2题:增减字符串匹配 第3题:数字的补数 第4题:Nim游戏 第5题:删除字符串中的所有相邻重复项 第6题:除数博弈 第7题:转换成小写字母 第8题:生成每种字符都是奇 ...

  6. 力扣刷题【20,21,26,27,35】

    - 20 有效的括号 21 合并两个有序链表 26 删除数组中的重复项 27. 移除元素 35. 搜索插入位置 20 有效的括号 使用replace一直替换 package top.lel.lc.ea ...

  7. 力扣K神图解算法数据结构解析10

    力扣K神图解算法数据结构点这里 十.分治算法 剑指07,重建二叉树 //时间O(n),空间O(n) //自己一直觉得这道题很难,没想到还是能够拿下,其实理论也清楚,前序遍历和中序遍历 //关键如下 / ...

  8. 力扣算法1~10题(js)

    标题 力扣算法 文章目录 标题 1.贩卖柠檬水,能否正确找零 错误 思路一: 解法一 缺点:耗时太长 解法二(大佬解法) 2.返回nums中和为target的两个数的下标 解法 3.两数相加 思路与算 ...

  9. 《LeetCode力扣练习》第10题 C语言版 (做出来就行,别问我效率。。。。)

    库你急哇,哈集美马戏特~~ 这道题很快啊,啪的一下,粘贴通过了,题目说你不讲武德,我说让这题耗子尾汁 作为每日一题其实难度还可以,毕竟我也是花了半分钟才复制粘贴提交通过的 这道难度为困难的题目确实很难 ...

最新文章

  1. C 语言编程 — 编程规范
  2. 关闭 Sublime Text 3 自动更新
  3. java获取数组最大最小值
  4. 区块链技术核心概念与原理讲解-Tiny熊
  5. 3D数学之柏林噪声(Perlin Noise)
  6. [转载] python字符串只留数字_Python工匠:数字与字符串(下)
  7. Linux设备管理(三):sysfs文件系统的功能及其应用
  8. 实验二+065+方绎杰
  9. element ui表单处理的简洁方法
  10. 大华 解码器上大屏代码事例
  11. 不定积分24个基本公式整理
  12. 微信聊天记录导出文本
  13. 2022中兴捧月算法挑战赛(RAW图像去噪)——初赛到决赛总结与反思
  14. VP9编解码标准知识总结
  15. 光遇挂_创作者与一束光的七年之约:陈星汉的Sky光遇详解
  16. 表格如何把边框线条加粗
  17. 丹佛机场自动行包系统案例
  18. 如何在Word中插入Notepad++样式的代码段
  19. 新世纪版五笔字根高清版
  20. 为什么hadoop没有slaves配置文件?

热门文章

  1. Mac上的日记软件——day one for mac,记录您每天的故事!
  2. rust 官服 时间_rust所有官服介绍 | 手游网游页游攻略大全
  3. 英雄联盟服务器维护2020.3.17,lol英雄联盟3月17日停机维护几点结束 2020年3.17lol英雄联盟更新维护什么内容?...
  4. C++星罗万象时钟罗盘
  5. icloud电脑设置_如何在Android上设置iCloud电子邮件访问
  6. T1004: 字符三角形(信息学一本通C++)
  7. 视频教程-Unity快速入门系列课程(第2部)-Unity3D
  8. 曾国藩谕纪泽(咸丰六年十月初二日)-珍惜光阴
  9. 【GOLDWAVE 信号源】
  10. 如何通过客户价值BI分析为银行提供价值