LeetCode:17. Letter Combinations of a Phone Number - Python
问题描述:
电话号码的字母组合
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明: 尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
问题分析:
很显然是,是回溯题目,递归求解,可以使用深度优先(dfs)的思想。
(1)用 index 记录 digits 字符串中的字符索引。
(2)用 paths 表示走过的字符,走到头,就保存到 res, 然后回溯当上一层,继续。
(3)这个过程相当于深度优先搜索。
Python3实现:
class Solution:def letterCombinations(self, digits):if not digits: return [] # 如果为空,返回空列表dict = {'1': [''],'2': ['a', 'b', 'c'],'3': ['d', 'e', 'f'],'4': ['g', 'h', 'i'],'5': ['j', 'k', 'l'],'6': ['m', 'n', 'o'],'7': ['p', 'q', 'r', 's'],'8': ['t', 'u', 'v'],'9': ['w', 'x', 'y', 'z']}# index 表示 digits字符串中的字符索引# paths 表示走过的字符串,走到头,就保存到 res, 然后回溯当上一个位置,继续。def dfs(dict, digits, index, paths, res): # 定义内部,递归函数if index == len(digits):res.append(paths)returnfor s in dict[digits[index]]:dfs(dict, digits, index + 1, paths + s, res)res = []dfs(dict, digits, 0, '', res)return resif __name__ == '__main__':digits = '92'solu = Solution()print(solu.letterCombinations(digits))
其他实现:
(1)使用Python functools 库中的 reduce 函数实现,注:经测试,这种方式最快(这三个方法)。
# @Time :2018/7/7
# @Author :LiuYinxing
from functools import reduceclass Solution:def letterCombinations(self, digits):if not digits: return [] # 如果为空,返回空列表dict = {'1': [''],'2': ['a', 'b', 'c'],'3': ['d', 'e', 'f'],'4': ['g', 'h', 'i'],'5': ['j', 'k', 'l'],'6': ['m', 'n', 'o'],'7': ['p', 'q', 'r', 's'],'8': ['t', 'u', 'v'],'9': ['w', 'x', 'y', 'z']}return reduce(lambda acc, digit: [x + y for x in acc for y in dict[digit]], digits, [''])if __name__ == '__main__':digits = '9562'solu = Solution()print(solu.letterCombinations(digits))
(2)使用Python itertools 库中的 product(求集合的笛卡尔积),注:经测试,这种方式最慢(这三个方法)。
# @Time :2018/7/7
# @Author :LiuYinxing
from itertools import productclass Solution:def letterCombinations(self, digits):if not digits: return [] # 如果为空,返回空列表dict = {'1': [''],'2': ['a', 'b', 'c'],'3': ['d', 'e', 'f'],'4': ['g', 'h', 'i'],'5': ['j', 'k', 'l'],'6': ['m', 'n', 'o'],'7': ['p', 'q', 'r', 's'],'8': ['t', 'u', 'v'],'9': ['w', 'x', 'y', 'z']}input = [dict[digit] for digit in digits] # 获取对应的字符return [''.join(i) for i in product(*input)] # 使用 product 做笛卡尔积if __name__ == '__main__':digits = '92'solu = Solution()print(solu.letterCombinations(digits))
欢迎指正哦。
LeetCode:17. Letter Combinations of a Phone Number - Python相关推荐
- [ LeetCode ] #17. Letter Combinations of a Phone Number(电话按键组合字符 C++ Python)
题目:17. Letter Combinations of a Phone Number Difficulty: Medium Given a string containing digits fro ...
- 【DFS】LeetCode 17. Letter Combinations of a Phone Number
LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...
- Leetcode 17 - Letter Combinations of a Phone Number
题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...
- Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节
1 题目理解 此道题目基于我们前几年用的,或许还有这几年用的九宫格输入法.九宫格上的每个数字对应着不同的字母,现在说,如果给定你一个数字,问你可以对应到多少种的字母组合? 这道题的解题方式,就是直接递 ...
- [LeetCode] 17. Letter Combinations of a Phone Number
题目内容 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. https://leetcode-cn.com/p ...
- LeetCode 17. Letter Combinations of a Phone Number
题意:给出数字串,输出对应的字母串组合,数字与字母的对应关系为手机上的 思路:使用递归,使用字符数组来存储递归选取时的字母,当到达数字串长度时,将字符数组转换为字符串 注意:数字串为空串时,输出为空 ...
- Leetcode 17. Letter Combinations of a Phone Number(python)
笨方法回溯啦 class Solution(object):def letterCombinations(self, digits):""":type digits: s ...
- 17. Letter Combinations of a Phone Number
集合类的,一看就是back track 只不过子集需要规定一下. 然后好在没有1 0 什么的..简单了很多 public class Solution {public List<String&g ...
- 17. Letter Combinations of a Phone Number 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
最新文章
- 在线作图|FAPROTAX数据库在线注释
- Leecode01. 两数之和——Leecode大厂热题100道系列
- 创业感悟:技术兄弟为什么一直没有起来(1)
- 一个程序员送给大学生的一些话
- css中相对定位和绝对定位
- 处理接口超时_架构设计 | 接口幂等性原则,防重复提交Token管理
- BZOJ 1007 水平可见直线
- Python秒求四位玫瑰数
- javaio_java------io基础(一)
- Abaqus6.14.4 Linux详细图文安装教程
- unity遇到的问题
- 在Caché中使用正则表达式
- 计算机内存是ram,电脑ram内存不足怎么办
- 百度网盘四种方法免费提速
- DSPE-PEG-cRGD,磷脂-聚乙二醇-环肽RGD,靶向穿膜肽RGD环肽供应
- 有道云笔记markdown字体增大、生成目录
- Go语言GoFrame开发框架
- 007 分数的基本性质(五下)
- ios开发证书CER文件、P12文件,mobileprovition许可文件的用途
- 400 名微软员工曝光薪资:4 万美元到 32 万美元不等!