diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 407596c0..584bcb2a 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List wordList) ``` ## Python - +``` +class Solution: + def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: + wordSet = set(wordList) + if len(wordSet)== 0 or endWord not in wordSet: + return 0 + mapping = {beginWord:1} + queue = deque([beginWord]) + while queue: + word = queue.popleft() + path = mapping[word] + for i in range(len(word)): + word_list = list(word) + for j in range(26): + word_list[i] = chr(ord('a')+j) + newWord = "".join(word_list) + if newWord == endWord: + return path+1 + if newWord in wordSet and newWord not in mapping: + mapping[newWord] = path+1 + queue.append(newWord) + return 0 +``` ## Go ## JavaScript