leetcode-17-电话号码的字母组合’
题目描述:

方法一:回溯
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-电话号码的字母组合’的更多相关文章
- Java实现 LeetCode 17 电话号码的字母组合
17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...
- [LeetCode] 17. 电话号码的字母组合(回溯)
题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...
- [LeetCode] 17. 电话号码的字母组合
题目描述:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 题目描述: 给定一个仅包含数字 2-9 的字符 ...
- LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)
题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...
- leetcode 17电话号码的字母组合
与子集70?类似,子集每次两个分支,本题每次k个分支,子集是第一次不push第二次push元素,本题是每次都push元素,因此,本题答案的长度都为k,子集题目为各种组合: /** res,level, ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- leetcode(js)算法之17电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母 示例: 输入:"23" 输出:[" ...
- Leetcode题库——17.电话号码的字母组合
@author: ZZQ @software: PyCharm @file: letterCombinations.py @time: 2018/10/18 18:33 要求:给定一个仅包含数字 2- ...
- leetcode题目17.电话号码的字母组合(中等)
题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出: ...
随机推荐
- [已解决]报错run `npm audit fix` to fix them, or `npm audit` for details
问题: added 246 packages from 681 contributors and audited 382 packages in 17.509s found 13 vulnerabil ...
- 构造+数位dp
参考博客: 题目链接: 题意:给定正整数a,b,k,你的任务是在所有满足a<=n<=b中的整数n中,统计有多少个满足n自身是k的倍数,且n的各位数字之和也是k的倍数. [思路] 这种题的固 ...
- mongodb入门篇
MongoDB 入门篇 分类: NoSQL, 故障解决 undefined 1.1 数据库管理系统 在了解MongoDB之前需要先了解先数据库管理系统 1.1.1 什么是数据? 数据(英语:data) ...
- 在IDEA中用Gradle构建项目时使用lombok以依赖出现出错
情景: 之情一直是使用Maven构建的项目并且导入依赖后都可以正常使用,但是在换成Gradle时出现了不论使用什么版本的lombok的依赖都会提示@Sl4j注解的log找不到,但是编辑界面是不会报错的 ...
- 2019-3-15-在-Windows-Defender-设置文件夹白名单提升-VisualStudio-编译速度
title author date CreateTime categories 在 Windows Defender 设置文件夹白名单提升 VisualStudio 编译速度 lindexi 2019 ...
- 2019_7_31python
练习 输入三条边长如果能构成三角形就计算周长和面积 import math a,b,c = input().split(',') a = float(a) b = float(b) c = floa ...
- MySQL高可用配置(主从复制)
主从复制包含两个步骤: 在 master 主服务器(组)上的设置,以及在 slave 从属服务器(组)上的设置. 环境: MASTER: 192.168.155.101SLAVE: 192.168.1 ...
- AWS cloudfront
添加新跳转的时候添加的参数 AcceptAccess-Control-Allow-OriginAuthorizationCloudFront-Forwarded-ProtoCloudFront-Vie ...
- PC端写的API接口和手机端APP联合调试
一.遇到问题的情况:项目框架:asp.net MVC5 ,写的给手机端调用的API接口. 二.自己在本地 IIS上部署项目,在手机端的请求服务器上把地址和端口换上本地部署的,如图所示 三.用管理员的身 ...
- 笔记47 Hibernate快速入门(四)
Hibernate注解,完成原来xml文件里的映射信息. 使用注解时,要修改hibernate.cfg.xml中的映射,不再是通过xml文件 <mapping class="hiber ...