zoukankan      html  css  js  c++  java
  • 栈和队列_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)
  • 相关阅读:
    解决Struts中文乱码问题总结
    多校第十场1009 CRB and String题解
    DirectX--Filter属性页的调用
    理解ThreadLocal类
    unity3d
    使用Java8提供的Duration类制作字幕时间轴调整工具
    算法练习--卡片游戏
    在eclipse中创建web项目
    testNg官方文档
    TestNG基本注解(注释)
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547376.html
Copyright © 2011-2022 走看看