单词接龙-图论127-python

news/2024/7/15 17:35:31 标签: 图论, leetcode, python

本题要求的是最短转换序列的长度,看到最短首先想到的就是广度优先搜索。想到广度优先搜索自然而然的就能想到图,但是本题并没有直截了当的给出图的模型,因此需要按照题意把它抽象成图的模型。

python">class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:        
        edge = dict() # 记录无向图中每个单词的连接单词
        visited = dict() # 记录单词是否被访问过,避免死循环
        res = 1 # beginWord本身为一个长度

        # 快速查找endWord是否在wordList中
        for word in wordList:
            edge[word] = []
        if endWord not in edge:
            return 0
        # 将beginWord纳入wordList中,以在后续建图过程中将其划入图中
        if beginWord not in edge:
            edge[beginWord] = []
            wordList = [beginWord] + wordList

        # 建图时间复杂度O(N*C*26),比两两单词判断(O(N*N*C))快
        for word in wordList:
            list_word = list(word)
            n = len(list_word)
            for i in range(n):
                tmp = list_word[i]
                for k in range(26):
                    list_word[i] = chr(ord('a') + k)
                    newWord = ''.join(list_word)
                    if newWord in edge and newWord != word:
                        edge[word].append(newWord)
                list_word[i] = tmp
        
        queue = [beginWord] # 以beginWord为起点,开始BFS过程

        while queue:
            current = len(queue)
            for _ in range(current):
                node = queue[0]
                queue.pop(0)
                visited[node] = True
                for vertex in edge[node]:
                    if vertex == endWord:
                        return res + 1
                    if vertex not in visited:
                        queue.append(vertex)
            res += 1
                       
        return 0

http://www.niftyadmin.cn/n/1541532.html

相关文章

C#备忘录

c# 小小备忘录 一、简述 备忘录,相信大家生活中都使用过,比如记笔记、手机备忘录等等,这些都是记录自己灵感时所想、定期内想做的事情,好像跑题了,说说我的备忘录吧,我的备忘录功能上也就是增删改查的操作&…

冗余连接-并查集684-python

并查集问题,在find和union函数的构造参考并查集的模板,same函数用于找到问题的答案。 class Solution:def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:n len(edges)parent [0] * n # 祖先数组:用于记录每一个…

机器人能否返回原点-模拟657-python

没看答案,只要向右移动和向左移动,向前移动和向后移动的次数一样,即可返回原点。 from collections import Counterclass Solution:def judgeCircle(self, moves: str) -> bool:move Counter(moves)return move[U] move[D] and move[L]…

用户体验分析: 以 “师路南通” 为例

1. 目标 针对师路南通 ,开展UX分析。 PS:对比另外2个学习网站: 1. UMU学习平台 ; 2. 学生安全教育平台 基于实例分析,体会用户体验设计的 7 条准则。 2. 要求 基于我们列出的 7 条UX评价准则,分析“师路南通…

冗余连接II-并查集685-python

并查集的应用,分为存在\不存在入度为2的顶点两种情况讨论。 附加的边指向根节点,则包括根节点在内的每个节点都有一个父节点,此时图中一定有环路;附加的边指向非根节点,则恰好有一个节点(即被附…

python的几个小程序

##九九乘法口诀 num11 while num1<10: num21 while num2<num1: print(num2,"*",num1,"",num1*num2,end" ") num21 print() num11 #矩形 width int(input("w:"))height int(input("h:"))num_height 1while num_he…

C#中判断扫描枪输入与键盘输入

大家好&#xff0c;这是我的第一篇博客&#xff0c;希望大家多多指教。 提出问题&#xff1a;在收货系统中&#xff0c;常常要用到扫描枪扫描条码输入到TextBox&#xff0c;当条码无法扫描时&#xff0c;需要手工输入。如果是扫描枪输入时&#xff0c;我们将自动去判读条码&…

Less 的使用方法

Less 的使用方法 Less 可以直接在浏览器端运行&#xff08;支持IE6、Webkit、Firefox&#xff09;&#xff0c;也可以借助Node.js或者Rhino在服务端运行。 Less是一种动态语言&#xff0c;无论是在浏览器端&#xff0c;还是在服务器端运行&#xff0c;最终还是需要编译成 CSS&a…