Python 在wordLadder中使用条件检查而不是交换来切换进程
我在leetcode中模拟了wordLadder问题的双向解决方案 给定两个单词(beginWord和endWord)和词典的单词列表,找出从beginWord到endWord的最短转换序列的长度,如下所示:Python 在wordLadder中使用条件检查而不是交换来切换进程,python,Python,我在leetcode中模拟了wordLadder问题的双向解决方案 给定两个单词(beginWord和endWord)和词典的单词列表,找出从beginWord到endWord的最短转换序列的长度,如下所示: 一次只能更改一个字母 每个转换的单词必须存在于单词列表中。请注意,beginWord不是经过转换的单词 注意: Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","co
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
- 如果没有这样的转换序列,则返回0
- 所有单词的长度都相同
- 所有单词仅包含小写字母字符
- 您可以假定单词列表中没有重复项
- 您可以假定起始字和结束字是非空的,并且不相同
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
示例2:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
解决方案
class Solution2(object):
def ladderLength(self, beginWord, endWord, wordList):
#base case
if (endWord not in wordList) or (not endWord) or (not beginWord) or (not wordList):
return 0
size = len(beginWord)
word_set = set(wordList)
forwards, backwards = {beginWord}, {endWord}
visited = set()
step = 0
while forwards and backwards:
step += 1 #treat the first word as step 1
if len(forwards) > len(backwards):
forwards, backwards = backwards, forwards #switch process
#logging.debug(f"step: {step}, forwards: {forwards}, backwords: {backwards}")
neighbors= set()
for word in forwards:#visit words on this level
if word in visited: continue
for i in range(size):
for c in 'abcdefghijklmnopqrstuvwxyz':
next_word = word[:i] + c + word[i+1:]
if next_word in backwards: return step + 1 #terminating case
if next_word in word_set: neighbors.add(next_word)
#logging.debug(f"next_word{next_word}, step: {step}")
visited.add(word) #add visited word as the final step
forwards = neighbors
#logging.debug(f"final: {step}")
return 0
切换过程的参考
if len(forwards) > len(backwards):
forwards, backwards = backwards, forwards #switch process
这个解决方案简洁但不直观,我尝试将其改为
if len(forwards) <= len(backward): current = forwards
else: current = backwards
neighbors = set()
for word in current:
.......
如果len(forwards)这称为agolythm<代码>向前
和向后
在这个解决方案中是一种双指针
的想法,总是选择较大的集合来做。这有助于更快地找到路径
关于你想用current
而不是切换forward
和backward
的问题,我认为这是不可能的。原因是,我们在逻辑中同时使用forward
和backward
,因此除了current
之外,您还应该提供一个类似另一个
的变量。但是当前的和另一个与向前的和向后的是一样的,所以你的想法行不通
在我看来,我觉得这个two-pointer
版本足够优雅和简洁,我个人喜欢它
我想另一种方法是,使用当前索引
与您的想法非常接近:
class Solution:
def ladderLength(self, beginWord, endWord, wordList):
#base case
if (endWord not in wordList) or (not endWord) or (not beginWord) or (not wordList):
return 0
size = len(beginWord)
word_set = set(wordList)
entries = [{beginWord}, {endWord}]
visited = set()
step = 0
cur = 0
while entries[cur] and entries[1-cur]:
step += 1 #treat the first word as step 1
if entries[1-cur] > entries[cur]: #switch process
cur ^= 1
neighbors= set()
for word in entries[cur]:#visit words on this level
if word in visited: continue
for i in range(size):
for c in 'abcdefghijklmnopqrstuvwxyz':
next_word = word[:i] + c + word[i+1:]
if next_word in entries[1-cur]: return step + 1 #terminating case
if next_word in word_set: neighbors.add(next_word)
visited.add(word) #add visited word as the final step
entries[cur] = neighbors
return 0
希望这对您有所帮助,如果您还有其他问题,请发表评论。:)