LeetCode解题之Letter Combinations of a Phone Number


原题

手机按键上每一个数字都相应了多个字母,如2相应了”abc”,现给出一个数字串。要求把当中的每一个数字都转化为相应的字母中的一个,列出全部的组合情况。

注意点:

  • 对结果的排列顺序没有要求

样例:

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

解题思路

把每一个数字相应的字母都当做树的节点,例如以下图。则所求结果就是从根节点到叶节点的全部的路径,採用深度优先遍历算法。

AC源代码

class Solution(object):digit2letters = {'2': "abc",'3': "def",'4': "ghi",'5': "jkl",'6': "mno",'7': "pqrs",'8': "tuv",'9': "wxyz",}def letterCombinations(self, digits):""":type digits: str:rtype: List[str]"""if not digits:return []result = []self.dfs(digits, "", result)return resultdef dfs(self, digits, current, result):if not digits:result.append(current)returnfor c in self.digit2letters[digits[0]]:self.dfs(digits[1:], current + c, result)if __name__ == "__main__":assert Solution().letterCombinations("23") == ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源代码。

转载于:https://www.cnblogs.com/jhcelue/p/7118195.html

LeetCode Letter Combinations of a Phone Number相关推荐

  1. leetCode:Letter Combinations of a Phone Number

    题目 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinatio ...

  2. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. 1 class Solution { 2 pub ...

  3. Leetcode 17 - Letter Combinations of a Phone Number

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

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

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

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

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

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

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

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

  8. [LeetCode] 17. Letter Combinations of a Phone Number

    题目内容 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. https://leetcode-cn.com/p ...

  9. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. 【异常】 ‘ascii‘ codec can‘t decode byte 0xe8 in position 2: ordinal not in range(128)
  2. 关于数据库内“表”的几点
  3. maven(三)最详细的profile的使用
  4. PostgreSQL示例demo
  5. MM引擎新应用——爱车加油记
  6. lvs和HA的高可用性
  7. 一个程序员的创业失败教训
  8. window.open打开新窗口被浏览器拦截的处理方法
  9. 论文浅尝 | 可建模语义分层的知识图谱补全方法
  10. postgresql集群方案hot standby初级测试(二) ——处理数据能力
  11. 诺基亚五摄手机终于发布了!拍摄能力无与伦比 售价699欧元
  12. c语言socket面试题,【C++工程师面试宝典】学习说明_互联网校招面试真题面经汇总_牛客网...
  13. 2019,一年一度的高考季来临,作文又成热门话题
  14. Backtrader(二十三)- 多股票回测
  15. 中国人应该知道的知识
  16. Neutron IPAM源码分析
  17. Lintcode:报数
  18. 盛迈坤电商:店铺获得流量有哪些方法
  19. LTE-UMTS长期演进与实践学习(1)——LTE基本架构
  20. 没比这更简单的了!安装PHP composer(windows环境)及简易使用

热门文章

  1. 无意中发现的MSDN软件下载网站
  2. LeetCode简单题目(#53 #58 #66 #67 #69 #70 #83 #88)-8道
  3. 摸清Linux日志处理的来龙去脉
  4. 职业程序员不必那么职业
  5. 转载: WMS、WFS、WCS、WPS、WMTS、WMSC、TMS等常见地图服务的区别
  6. 转: iptables数据包处理流程
  7. 使用GDAL打开裸数据(RAW)
  8. 开源GIS(十九)——WKT、WKB与GeoJSON
  9. 高德开放平台定位功能对接
  10. 4个不错的Flash开源项目