题目描述:

方法一:回溯

class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
phone = {'': ['a', 'b', 'c'], '': ['d', 'e', 'f'], '': ['g', 'h', 'i'], '': ['j', 'k', 'l'], '': ['m', 'n', 'o'], '': ['p', 'q', 'r', 's'], '': ['t', 'u', 'v'], '': ['w', 'x', 'y', 'z']}
def backtrack(combination, next_digits):
# if there is no more digits to check
if len(next_digits) == 0:
# the combination is done
output.append(combination)
# if there are still digits to check
else:
# iterate over all letters which map
# the next available digit
for letter in phone[next_digits[0]]:
# append the current letter to the combination
# and proceed to the next digits
backtrack(combination + letter, next_digits[1:])
output = []
if digits: backtrack("", digits)
return output

二:

class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
m = {'': ['a', 'b', 'c'], '': ['d', 'e', 'f'], '': ['g', 'h', 'i'], '': ['j', 'k', 'l'], '': ['m', 'n', 'o'], '': ['p', 'q', 'r', 's'], '': ['t', 'u', 'v'], '': ['w', 'x', 'y', 'z']}
if not digits:
return []
ls1 = ['']
for i in digits:
ls1 = [x + y for x in ls1 for y in m[i]]
return ls1

leetcode-17-电话号码的字母组合’的更多相关文章

  1. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  2. [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###

    描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...

  3. [LeetCode] 17. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

  4. [LeetCode] 17. 电话号码的字母组合

    题目描述:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 题目描述: 给定一个仅包含数字 2-9 的字符 ...

  5. LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...

  6. leetcode 17电话号码的字母组合

    与子集70?类似,子集每次两个分支,本题每次k个分支,子集是第一次不push第二次push元素,本题是每次都push元素,因此,本题答案的长度都为k,子集题目为各种组合: /** res,level, ...

  7. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  8. leetcode(js)算法之17电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母 示例: 输入:"23" 输出:[" ...

  9. Leetcode题库——17.电话号码的字母组合

    @author: ZZQ @software: PyCharm @file: letterCombinations.py @time: 2018/10/18 18:33 要求:给定一个仅包含数字 2- ...

  10. leetcode题目17.电话号码的字母组合(中等)

    题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出: ...

随机推荐

  1. VisualStuido中将C#脚本封装打包DLL并调用

    DLL (Dynamic Link Library)---动态链接库 首先了解下使用DLL的优势,程序运行时不用加载所有代码,只有运行到引用时,才从DLL库中取出.并且使用DLL文件还可以减小程序体积 ...

  2. python--hmac

    检测一下客户端是否合法,不依靠登录认证.这样即使知道服务端ip和端口,客户端也不能随便就连接服务端实现机制是在服务端中设置了密钥,借用了hmac os 两个模块 import socket impor ...

  3. springboot java.util.NoSuchElementException: No value present 异常处理

    使用jpa查询的时候,如果查询不到数据,就会返回这个错误,下面是处理方法. @Override public User findByEmail(String email) { User user = ...

  4. Java KMP算法代码

    1. KMP 算法(字符串匹配算法)较 BF(朴素的字符串匹配)算法有哪些改进 1) 在主串和子串匹配的过程中,主串不再回退,只改变子串的比较位置. 2) 为子串生成对应的next数组,每次匹配失败, ...

  5. SQL Server 中根据字段值查询其所在的表、字段

    DECLARE @what varchar(800)SET @what='123456' --要搜索的字符串   DECLARE @sql varchar(8000)   DECLARE TableC ...

  6. bzoj1010题解

    [解题思路] 设s[i]=i+∑c[j](j∈[1,n]∩N) 易得转移方程f[i]=min{f[j]+(s[i]-s[j]-L-1)2},朴素算法复杂度O(n2). 考虑斜率优化:记T[i]=s[i ...

  7. python数据结构之二叉树的统计与转换实例

    python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...

  8. idea不断提示=========>This file is indented with tabs instead of 4 spaces

    file->other settings ->default settings

  9. JSON Web Token (JWT)生成Token及解密实战。

    昨天讲解了JWT的介绍.应用场景.优点及注意事项等,今天来个JWT具体的使用实践吧. 从JWT官网支持的类库来看,jjwt是Java支持的算法中最全的,推荐使用,网址如下. https://githu ...

  10. 对Springdata模块的简单理解

    有关于Spring对数据库的操作属于为Spring中的Springdata模块,对数据库的操作.Spring对JDBC和Mybatis都有封装与简化 可以从以下角度学习研究 SpringData: 1 ...