题目内容

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/

Given a string containing digits from 2-9inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Example:Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

题目思路

这道题目的思路在于可以把每一次对于字符的匹配看成一个双循环组成,一个是循环目前的res,另一个是循环目标数字所代表的字符串。初始的res被设定成含有一个空字符串的list,循环后更新结果,并进行下一轮循环。


程序代码

class Solution(object):def letterCombinations(self, digits):""":type digits: str:rtype: List[str]"""if not digits:return []num={'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}res=['']for i in digits:temp=[m+n for m in res for n in num[i]]res=tempreturn res

[LeetCode] 17. Letter Combinations of a Phone Number相关推荐

  1. 【DFS】LeetCode 17. Letter Combinations of a Phone Number

    LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...

  2. [ 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 ...

  3. Leetcode 17 - Letter Combinations of a Phone Number

    题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...

  4. Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节

    1 题目理解 此道题目基于我们前几年用的,或许还有这几年用的九宫格输入法.九宫格上的每个数字对应着不同的字母,现在说,如果给定你一个数字,问你可以对应到多少种的字母组合? 这道题的解题方式,就是直接递 ...

  5. LeetCode 17. Letter Combinations of a Phone Number

    题意:给出数字串,输出对应的字母串组合,数字与字母的对应关系为手机上的 思路:使用递归,使用字符数组来存储递归选取时的字母,当到达数字串长度时,将字符数组转换为字符串 注意:数字串为空串时,输出为空 ...

  6. Leetcode 17. Letter Combinations of a Phone Number(python)

    笨方法回溯啦 class Solution(object):def letterCombinations(self, digits):""":type digits: s ...

  7. LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】

    LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...

  8. LeetCode 17. Letter Combinations of a Phone Number--笔试题--C++,Python解法

    题目地址:Letter Combinations of a Phone Number - LeetCode Given a string containing digits from 2-9 incl ...

  9. LeetCode:17. Letter Combinations of a Phone Number - Python

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

最新文章

  1. 飞机大战html游戏全代码js、jquery操作
  2. 凯文·凯利:未来很美好,今天仍是Day1
  3. ATMEGA8 DIP-28面包板实验
  4. .NET通过RFC读取SAP数据
  5. 程序员相亲记之准博士mm
  6. 线程本地ThreadLocal的介绍与使用!
  7. 操作多个表_1_记录集的叠加
  8. SAP ABAP报表依赖设计原理详解
  9. 单元测试:如何编写可测试的代码及其重要性
  10. python 循环 覆盖之前print内容_Python爬虫第二战---爬取500px图片
  11. jpa findone怎么用_Jpa VS MyBatis,你用哪个?
  12. 【nodejs原理源码赏析(9)】用node-ssh实现轻量级自动化部署
  13. spring boot guava cache 缓存学习
  14. MySQL学习(三、分组查询和多表查询)
  15. unix下source的使用
  16. 荣耀9.0系统怎么无需root激活XPOSED框架的教程
  17. 微信小程序入门开发教程
  18. Android软件开发之获取通讯录联系人信息
  19. 算法工程师月薪多少钱,算法工程师岗位需要学习哪些知识?
  20. 亚马逊后台操作不容措施旺季~~~~~~

热门文章

  1. ROS Couldn‘t find executable named xxx.py问题
  2. 蓝桥杯十大常见天阶功法——虫之呼吸.贰之型.二分
  3. IOS开发百度地图API-用点生成路线,导航,气泡响应
  4. 堆排序(最小堆)C++
  5. 楼层标高怎么引上去_高层建筑施工标高传递的简易方法
  6. Struts2介绍(一个大的工具库)
  7. C++ 身份证设定(复合类+拷贝构造)
  8. git基础配置/SSH
  9. SpringCloud Gateway微服务网关实战与源码分析-上
  10. 隐私计算--25--联邦学习激励机制