126. Word Ladder II

126. Word Ladder II

python 复制代码
class Solution:
    def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
        wordList=set(wordList)

        res=[]
        layer={beginWord:[[beginWord]]}

        while layer:
            newlayer=defaultdict(list)
            for w in layer:
                if w==endWord:
                    res.extend(k for k in layer[w])
                else:
                    for i in range(len(w)):
                        for c in string.ascii_lowercase:
                            neww=w[:i]+c+w[i+1:]
                            if neww in wordList:
                                newlayer[neww]+=[j+[neww] for j in layer[w]]
                
            wordList-=set(newlayer.keys())
            layer=newlayer
        return res
python 复制代码
class Solution:
    def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
        wordList=set(wordList)

        if endWord not in wordList:
            return []

        wordList.add(beginWord)
        wordDict=defaultdict(list)

        for word in wordList:
            for i in range(len(word)):
                pattern = word[:i]+'*'+word[i+1:]
                wordDict[pattern].append(word)
        
        queue=deque([(beginWord,1)])
        visited,adjList=defaultdict(set),defaultdict(list)
        visited[beginWord]=0
        numStep=1

        while queue:
            currWord,step=queue.popleft()

            if currWord==endWord:
                numStep=step
                break
            
            for i in range(len(currWord)):
                pattern=currWord[:i]+'*'+currWord[i+1:]
                if pattern in wordDict:
                    for word in wordDict[pattern]:
                        if word not in visited or visited[word]==step+1:
                            adjList[word].append(currWord)
                        
                            if word not in visited:
                                queue.append((word,step+1))
                                visited[word]=step+1

        if numStep==1:return []
        result=[]
        array=[endWord]

        self.backtrack(result,array, numStep,beginWord,adjList)
        return result

    def backtrack(self,result,array,numStep,beginWord,adjList):
        if len(array)==numStep and array[-1]==beginWord:
            temp=array.copy()
            result.append(temp[::-1])
            return 
        
        for word in adjList[array[-1]]:
            array.append(word)
            self.backtrack(result,array,numStep,beginWord, adjList)
            array.pop()

卡内存用回溯写法

相关推荐
小欣加油39 分钟前
leetcode 329 矩阵中的最长递增路径
c++·算法·leetcode·矩阵·深度优先·剪枝
Emilia486.42 分钟前
【Leetcode&nowcode&数据结构】单链表的应用(初阶)
c语言·数据结构·算法·leetcode
仰泳的熊猫1 小时前
LeetCode:700. 二叉搜索树中的搜索
数据结构·c++·算法·leetcode
代码充电宝2 小时前
LeetCode 算法题【中等】189. 轮转数组
java·算法·leetcode·职场和发展·数组
微笑尅乐2 小时前
从递归到迭代吃透树的层次——力扣104.二叉树的最大深度
算法·leetcode·职场和发展
im_AMBER2 小时前
Leetcode 28
算法·leetcode
And_Ii3 小时前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
冷月葬花~4 小时前
day24
数据结构·算法·leetcode
仰泳的熊猫5 小时前
LeetCode:538. 把二叉搜索树转换为累加树/1038. 从二叉搜索树到更大和树
数据结构·c++·算法·leetcode
Swift社区6 小时前
LeetCode 399 除法求值
算法·leetcode·职场和发展