mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
添加127. 单词接龙的Java版本的双向BFS代码
This commit is contained in:
@ -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<String> 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<String> 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<String> queueLeft = new ArrayDeque<>(), queueRight = new ArrayDeque<>();
|
||||
queueLeft.add(beginWord);
|
||||
queueRight.add(endWord);
|
||||
|
||||
// 从 beginWord 开始 BFS 时把遍历到的节点存入 hashSetLeft 中
|
||||
// 从 endWord 开始 BFS 时把遍历到的节点存入 hashSetRight 中
|
||||
Set<String> 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 {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user