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)
  • 相关阅读:
    IOS使用 swizzle 解决一些错误
    Objective-C的hook方案(一): Method Swizzling
    jmeter录制Chrome浏览器https请求进行压力测试
    FIDDLER导出JMX文件,JMETER打开导出的JMX报错的解决方式
    Fiddler的PC端与手机端抓包配置步骤
    初识中间件之消息队列--提高服务性能
    Python虚拟环境配置应用
    jmeter三种阶梯式加压
    JMETER-正则表达式提取与查看变量是否提取正确
    jmeter的线程数,并发用户数,TPS,RPS 关系解说
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547376.html
Copyright © 2011-2022 走看看