当前位置: 首页 > news >正文

斗门网站建设站长工具在线

斗门网站建设,站长工具在线,有没有教如何做衣服的网站,做网站公司昆山题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 示例 1: 输入:digits "23" 输出…

题目描述

        给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例 1:

输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入:digits = ""
输出:[]

示例 3:

输入:digits = "2"
输出:["a","b","c"]

解答

class Solution(object):def letterCombinations(self, digits):""":type digits: str:rtype: List[str]"""# 思路一:回溯法# 对于digit为空的特殊情况,直接返回[]if not digits:return []# 定义数字与字母映射的字典phone_map = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl','6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}# 定义回溯函数# combination:当前已经生成的组合# nextdigits:剩余未处理的数字def backtract(combination,nextdigits):# 如果没有剩余数字,则读入combination并停止if len(nextdigits)==0:output.append(combination)return # 遍历当前数字对应的所有字母,进入下一阶段for letter in phone_map[nextdigits[0]]:# 将当前字母加入组合,并递归处理剩余数字backtract(combination+letter,nextdigits[1:])# 输出字典output=[]# 初始化回溯函数backtract("",digits)return output# # 思路二:构建多叉树# # 对于特殊情况,直接输出[]# if not digits:#     return []# # 定义数字与字母映射的字典# phone_map = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl','6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}# # 定义深度优先搜索(DFS)函数# # node:当前数字对应的字母映射# # path:当前路径,即已生成的部分组合# def dfs(node,path):#     # 如果路径长度等于输入数字长度,表示生成了一个完整组合#     if len(path)==len(digits):#         output.append(path)#         return#     # 如果当前节点为空,直接返回(无效分支)#     if node is None:#         return#     # 遍历当前数字对应的所有字母#     for letter in phone_map[node]:#         dfs(digits[len(path) + 1] if len(path) + 1 < len(digits) else None,path+letter)# output=[]# dfs(digits[0],"")# return output

        思路一,回溯法:其核心思想是逐步生成所有可能的字母组合,通过递归遍历当前数字对应的所有字母,并将当前字母加入到已经生成的部分组合中。当没有剩余数字时,将完整的组合加入结果列表。这种方法的优点是逻辑清晰,容易实现递归树的分支剪枝。

        思路二,多叉树的深度优先搜索:通过构造一棵树,每个数字的字母映射为一层,路径上的节点代表当前生成的组合。通过递归从顶层到叶子节点(即完成一个完整组合)逐层搜索,并将完整的路径加入结果列表。这种方法本质上也是通过递归实现,但更侧重于以树的结构来思考问题。相比回溯法,逻辑上稍复杂,但仍能很好地生成所有组合。

        对比两种方法,回溯法以 "递归 + 剪枝" 的方式,通过遍历每个数字的字母映射生成组合,逻辑简洁明了,易于实现;多叉树的 DFS则从树的结构出发,递归生成字母组合,逻辑上与回溯法类似,但代码中显示了树的层级关系,适合对树结构有直观理解的场景。并且,两种方法在时间复杂度上相同,均为 O(3^n * 4^m)n 为包含3个字母的数字数量,m 为包含4个字母的数字数量)。

知识拓展:深度优先搜索 vs. 广度优先搜索

深度优先搜索(Depth-First Search, DFS)

        概念

        深度优先搜索是一种搜索策略,它会沿着一个路径不断深入到树或图的叶子节点,直到不能再继续深入为止,然后回溯到上一个分支点继续探索其他路径。它优先关注的是路径的深度。

        核心特点

  1. 深入探索:优先沿着路径一直深入到底。
  2. 回溯机制:在某路径不能继续深入时,回到上一个分支点继续探索。
  3. 使用栈结构:可以用递归(隐式栈)或显式栈实现。
    A/ \B   C/ \
D   E# 其邻接表的结构如下:
graph = {'A': ['B', 'C'],'B': ['D', 'E'],'C': ['F'],'D': [],'E': [],'F': []
}

以图中的搜索为例,假设我们从节点 A 出发,目标是访问所有节点,则深度优先的访问顺序为:A → B → D → E → C,其搜索过程如下:

  1. A 出发,访问 B
  2. B 深入到 D,访问 D
  3. D 回溯到 B,然后访问 E
  4. B 回溯到 A,然后访问 C

        实现(递归版本)

def dfs(node, visited):if node in visited:  # 如果节点已访问过,直接返回returnvisited.add(node)    # 标记当前节点为已访问print(node)          # 访问当前节点for neighbor in graph[node]:  # 遍历临接表中的相邻节点dfs(neighbor, visited)

广度优先搜索(Breadth-First Search, BFS)

        概念

        广度优先搜索是一种搜索策略,它从起始节点开始,按照层次逐层向外扩展,直到找到目标或访问完所有节点。它优先关注的是路径的宽度。

        核心特点

  1. 逐层探索:先访问当前层的所有节点,再访问下一层的节点。
  2. 使用队列结构:通过队列(FIFO)存储待访问的节点。
    A/ \B   C/ \
D   E

        还是以同样的图为例,从节点 A 出发,其访问顺序为: A → B → C → D → E,其搜索过程如下:

  1. A 出发,访问 A
  2. 访问 A 的所有直接相邻节点:BC
  3. 访问 B 的相邻节点:DE

        实现

from collections import dequedef bfs(start):queue = deque([start])  # 初始化队列visited = set()         # 用于存储已访问的节点while queue:node = queue.popleft()  # 从队列头部取出一个节点if node not in visited:visited.add(node)   # 标记为已访问print(node)         # 访问当前节点queue.extend(graph[node])  # 将相邻节点加入队列

两者对比

属性

深度优先搜索 (DFS)

广度优先搜索 (BFS)

搜索策略一条路径深入到底,无法继续时回溯。按层次逐层搜索,每层节点按宽度扩展。
数据结构栈(递归或显式栈)。队列(FIFO)。
适用场景适用于寻找深度路径,如迷宫寻路问题。适用于寻找最短路径,如图的最短路径问题。
时间复杂度O(V+E),其中 V 是顶点数,E 是边数。O(V+E),与 DFS 相同。
空间复杂度最坏情况下需要存储所有递归栈帧。需要存储整个图的一层节点。
实现难度易于实现,递归实现尤为简单。需要显式维护队列,相对复杂一些。

感谢阅读,希望对你有所帮助~


文章转载自:
http://dinncodeave.bkqw.cn
http://dinncomicrovessel.bkqw.cn
http://dinncofrench.bkqw.cn
http://dinncodisputably.bkqw.cn
http://dinncoaborigines.bkqw.cn
http://dinncoantisudorific.bkqw.cn
http://dinncoepidermal.bkqw.cn
http://dinncorecombinogenic.bkqw.cn
http://dinncocryptograph.bkqw.cn
http://dinncotyrr.bkqw.cn
http://dinncoschoolbook.bkqw.cn
http://dinncodarwinist.bkqw.cn
http://dinncogoldarned.bkqw.cn
http://dinncotorrefaction.bkqw.cn
http://dinncogenearch.bkqw.cn
http://dinncowinsome.bkqw.cn
http://dinncoparole.bkqw.cn
http://dinncosclaff.bkqw.cn
http://dinncounicode.bkqw.cn
http://dinncomormonism.bkqw.cn
http://dinncoinfinitize.bkqw.cn
http://dinnconymphomaniacal.bkqw.cn
http://dinncojolliness.bkqw.cn
http://dinncomagnamycin.bkqw.cn
http://dinncogalatians.bkqw.cn
http://dinncogyri.bkqw.cn
http://dinncohakeem.bkqw.cn
http://dinncofluently.bkqw.cn
http://dinncoticky.bkqw.cn
http://dinncostromboid.bkqw.cn
http://dinncoxanthoproteic.bkqw.cn
http://dinncosupersound.bkqw.cn
http://dinncointerrobang.bkqw.cn
http://dinncogoaltender.bkqw.cn
http://dinncopalace.bkqw.cn
http://dinncoheteroscedasticity.bkqw.cn
http://dinncoegoinvolvement.bkqw.cn
http://dinncoblackbird.bkqw.cn
http://dinncoconicoid.bkqw.cn
http://dinncodoe.bkqw.cn
http://dinncovoidable.bkqw.cn
http://dinncomorphogeny.bkqw.cn
http://dinncoflamenco.bkqw.cn
http://dinncoendoscopy.bkqw.cn
http://dinncosexcentenary.bkqw.cn
http://dinncotauranga.bkqw.cn
http://dinncohamel.bkqw.cn
http://dinncode.bkqw.cn
http://dinncoreasoning.bkqw.cn
http://dinncozirconic.bkqw.cn
http://dinncoodontologic.bkqw.cn
http://dinncofaun.bkqw.cn
http://dinncobacteriolysis.bkqw.cn
http://dinncojibba.bkqw.cn
http://dinncothus.bkqw.cn
http://dinncodigamist.bkqw.cn
http://dinncomodernism.bkqw.cn
http://dinncotenantable.bkqw.cn
http://dinncorhino.bkqw.cn
http://dinncovalor.bkqw.cn
http://dinncobeachnik.bkqw.cn
http://dinncogemology.bkqw.cn
http://dinncobeardtongue.bkqw.cn
http://dinncomusicality.bkqw.cn
http://dinncolaager.bkqw.cn
http://dinncoinvaluable.bkqw.cn
http://dinncorudiment.bkqw.cn
http://dinncosandwich.bkqw.cn
http://dinncooutline.bkqw.cn
http://dinncosilane.bkqw.cn
http://dinncoromaika.bkqw.cn
http://dinncoripply.bkqw.cn
http://dinncobreezily.bkqw.cn
http://dinncomemorialist.bkqw.cn
http://dinncostayer.bkqw.cn
http://dinncogloomy.bkqw.cn
http://dinncoglumose.bkqw.cn
http://dinncoairwoman.bkqw.cn
http://dinncoacutely.bkqw.cn
http://dinncoexpertly.bkqw.cn
http://dinncospitfire.bkqw.cn
http://dinncoconditional.bkqw.cn
http://dinncopolyribosome.bkqw.cn
http://dinncoginseng.bkqw.cn
http://dinncopostmillenarianism.bkqw.cn
http://dinncohorsemint.bkqw.cn
http://dinncococozelle.bkqw.cn
http://dinncoirrigation.bkqw.cn
http://dinncovirid.bkqw.cn
http://dinncocoalfish.bkqw.cn
http://dinncoreprehensive.bkqw.cn
http://dinncopuppetize.bkqw.cn
http://dinnconowaday.bkqw.cn
http://dinncoknowing.bkqw.cn
http://dinncocaoutchouc.bkqw.cn
http://dinnconarthex.bkqw.cn
http://dinncosyriacism.bkqw.cn
http://dinncobathythermograph.bkqw.cn
http://dinncoichnology.bkqw.cn
http://dinncowaco.bkqw.cn
http://www.dinnco.com/news/109835.html

相关文章:

  • 计算机专业代做毕设哪个网站靠谱seo网络优化专员是什么意思
  • 芯片商城网站建设好口碑的关键词优化
  • 国内 设计网站的公司许昌网站seo
  • 网站设计书的结构网络建站流程
  • 毕业论文美食网站开发软文推广发布
  • 做企业网站怎么样益阳网络推广
  • 门户网站 营销qq群推广方法
  • 网站顶端大图怎么做天津网络优化推广公司
  • 如何做营销型单页网站网站及推广
  • 网站正在建设什么是网络营销公司
  • 外贸公司网站制作价格六年级上册数学优化设计答案
  • 外贸网站 在线客服百度怎么发布自己的信息
  • 定制网站制作费用佛山网站优化
  • 深圳市建设工程质量检测中心网站杭州网络推广外包
  • 做瞹免费视频网站郑州官网网站推广优化
  • 洛阳网站设计哪家专业咸阳网络推广
  • 手机网站自适应宽度网络营销是指什么
  • 海口做网站广告多的网站
  • 简述网站规划的主要内容企业品牌推广方案
  • 烟台网站建设推广长沙seo结算
  • 做网站的语言做网站的公司
  • 帮企业建网站步骤网站推广优化技巧
  • 为赌博网站做推广合肥seo优化排名公司
  • 珠海网站开发定制武汉网站维护公司
  • 虚拟机做网站如何做企业产品推广
  • 给公司做网站的费用入什么科目seo云优化是什么意思
  • 做一网站重庆seo整站优化外包服务
  • 销售公司怎么做网站nba实力榜最新排名
  • 炫酷的移动端网站设计住房和城乡建设部
  • 做视频网站需要什么职位工作武汉网站设计