栈和队列_leetcode127

class Solution(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""


pair = (beginWord,1)
queue = []
queue.append(pair)

while queue:
curPair = queue.pop(0)
curWord = curPair[0]
curStep = curPair[1]

if curWord == endWord:
return curStep

nextWordList = self.getNextWordList(curWord,wordList)
for nextWord in nextWordList:
nextPair = (nextWord,curStep+1)
queue.append(nextPair)

return 0


def getNextWordList(self,curWord,wordList):
nextWordList = []

for word in wordList:
if self.isNextWord(curWord,word):
nextWordList.append(word)

return nextWordList

def isNextWord(self,curWord,nextWord):
diff = 0

for i in range(len(curWord)):
if curWord[i] != nextWord[i]:
diff += 1
return diff == 1

class Solution2(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""

visit = {}


for word in wordList :
visit[word] = False

pair = (beginWord,1)
queue = []
queue.append(pair)



while queue:
curPair = queue.pop(0)
curWord = curPair[0]
curStep = curPair[1]

if curWord == endWord:
return curStep

nextWordList = self.getNextWordList(curWord,wordList)

for nextWord in nextWordList:
if visit[nextWord] == False:
nextPair = (nextWord,curStep+1)
queue.append(nextPair)
visit[nextWord] = True


return 0


def getNextWordList(self,curWord,wordList):
nextWordList = []

for word in wordList:
if self.isNextWord(curWord,word):
nextWordList.append(word)

return nextWordList

def isNextWord(self,curWord,nextWord):
diff = 0

for i in range(len(curWord)):
if curWord[i] != nextWord[i]:
diff += 1

return diff == 1

class Solution3(object):
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""

visit = {}
wordList = set(wordList)

for word in wordList :
visit[word] = False

pair = (beginWord,1)
queue = []
queue.append(pair)



while queue:
curPair = queue.pop(0)
curWord = curPair[0]
curStep = curPair[1]

if curWord == endWord:
return curStep

nextWordList = self.getNextWordList(curWord, wordList)
for nextWord in nextWordList:
if visit[nextWord] == False:
nextPair = (nextWord,curStep+1)
queue.append(nextPair)
visit[nextWord] = True
wordList.remove(nextWord)

return 0


def getNextWordList(self,curWord,wordList):
nextWordList = []

for word in wordList:
if self.isNextWord(curWord,word):
nextWordList.append(word)

return nextWordList

def isNextWord(self,curWord,nextWord):
diff = 0

for i in range(len(curWord)):
if curWord[i] != nextWord[i]:
diff += 1

return diff == 1

# 这种数据过滤的优化思路真的吊class Solution4(object): def ladderLength(self, beginWord, endWord, wordList): """ :type beginWord: str :type endWord: str :type wordList: List[str] :rtype: int """ wordset = set(wordList) bfs = collections.deque() bfs.append((beginWord, 1)) while bfs: word, length = bfs.popleft() if word == endWord: return length for i in range(len(word)): for c in "abcdefghijklmnopqrstuvwxyz": newWord = word[:i] + c + word[i + 1:] if newWord in wordset and newWord != word: wordset.remove(newWord) bfs.append((newWord, length + 1)) return 0class Solution5(object): def ladderLength(self, beginWord, endWord, wordList): """ :type beginWord: str :type endWord: str :type wordList: List[str] :rtype: int """ visit = {} wordList = set(wordList) for word in wordList : visit[word] = False pair = (beginWord,1) queue = [] queue.append(pair) while queue: curPair = queue.pop(0) curWord = curPair[0] curStep = curPair[1] if curWord == endWord: return curStep nextWordList = self.getNextWordList(curWord, wordList) for nextWord in nextWordList: if visit[nextWord] == False: nextPair = (nextWord,curStep+1) queue.append(nextPair) visit[nextWord] = True wordList.remove(nextWord) return 0 def getNextWordList(self,curWord,wordList): nextWordList = [] for word in wordList: if self.isNextWord(curWord,word): nextWordList.append(word) return nextWordList def isNextWord(self,curWord,nextWord): diff = 0 for i in range(len(curWord)): if curWord[i] != nextWord[i]: diff += 1 return diff == 1b = "hit"e = "cog"wl = ["hot","dot","dog","lot","log","cog"]wl2 = ["hot","dot","dog","lot","log"]s = Solution2()print s.ladderLength(b,e,wl)
posted @ 2019-03-17 16:08  AceKo  阅读(163)  评论(0编辑  收藏  举报