From 5dd403e69ab53c363799e0182e6443e2fde99d7a Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 11:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0127.=20=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8E=A5=E9=BE=99=E7=9A=84Java=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=8F=8C=E5=90=91BFS=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0127.单词接龙.md | 67 ++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 7ffd6a21..b4078913 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -16,7 +16,7 @@ * 转换过程中的中间单词必须是字典 wordList 中的单词。 * 给你两个单词 beginWord 和 endWord 和一个字典 wordList ,找到从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0。 -  + 示例 1: * 输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] @@ -134,7 +134,71 @@ public int ladderLength(String beginWord, String endWord, List wordList) } ``` +## Java 双向BFS + +```java +class Solution { + // 判断单词之间是否之差了一个字母 + public boolean isValid(String currentWord, String chooseWord) { + int count = 0; + for (int i = 0; i < currentWord.length(); i++) + if (currentWord.charAt(i) != chooseWord.charAt(i)) ++count; + return count == 1; + } + + public int ladderLength(String beginWord, String endWord, List wordList) { + if (!wordList.contains(endWord)) return 0; // 如果 endWord 不在 wordList 中,那么无法成功转换,返回 0 + + // ansLeft 记录从 beginWord 开始 BFS 时能组成的单词数目 + // ansRight 记录从 endWord 开始 BFS 时能组成的单词数目 + int ansLeft = 0, ansRight = 0; + + // queueLeft 表示从 beginWord 开始 BFS 时使用的队列 + // queueRight 表示从 endWord 开始 BFS 时使用的队列 + Queue queueLeft = new ArrayDeque<>(), queueRight = new ArrayDeque<>(); + queueLeft.add(beginWord); + queueRight.add(endWord); + + // 从 beginWord 开始 BFS 时把遍历到的节点存入 hashSetLeft 中 + // 从 endWord 开始 BFS 时把遍历到的节点存入 hashSetRight 中 + Set hashSetLeft = new HashSet<>(), hashSetRight = new HashSet<>(); + hashSetLeft.add(beginWord); + hashSetRight.add(endWord); + + // 只要有一个队列为空,说明 beginWord 无法转换到 endWord + while (!queueLeft.isEmpty() && !queueRight.isEmpty()) { + ++ansLeft; + int size = queueLeft.size(); + for (int i = 0; i < size; i++) { + String currentWord = queueLeft.poll(); + // 只要 hashSetRight 中存在 currentWord,说明从 currentWord 可以转换到 endWord + if (hashSetRight.contains(currentWord)) return ansRight + ansLeft; + for (String chooseWord : wordList) { + if (hashSetLeft.contains(chooseWord) || !isValid(currentWord, chooseWord)) continue; + hashSetLeft.add(chooseWord); + queueLeft.add(chooseWord); + } + } + ++ansRight; + size = queueRight.size(); + for (int i = 0; i < size; i++) { + String currentWord = queueRight.poll(); + // 只要 hashSetLeft 中存在 currentWord,说明从 currentWord 可以转换到 beginWord + if (hashSetLeft.contains(currentWord)) return ansLeft + ansRight; + for (String chooseWord : wordList) { + if (hashSetRight.contains(chooseWord) || !isValid(currentWord, chooseWord)) continue; + hashSetRight.add(chooseWord); + queueRight.add(chooseWord); + } + } + } + return 0; + } +} +``` + ## Python + ``` class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: @@ -301,3 +365,4 @@ function diffonechar(word1: string, word2: string): boolean { +