【Leetcode】【python】Letter Combinations of a Phone Number
来源:互联网 发布:电脑去水印软件 编辑:程序博客网 时间:2024/06/05 07:40
题目大意
输入手机键盘的数字,组合所有可能的字母。
解题思路
DFS深度优先
代码
class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ def dfs(num, string, res): print string, res if num == length: res.append(string) return for letter in dict[digits[num]]: print letter dfs(num+1, string+letter, res) dict = {'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'] } res = [] length = len(digits) if length == 0: return [] dfs(0, '', res) return res
总结
该题目的标签是:回溯法回溯法采用试错的思想,它尝试分步的去解决一个问题。在分步解决问题的过程中,当它通过尝试发现现有的分步答案不能得到有效的正确的解答的时候,它将取消上一步甚至是上几步的计算,再通过其它的可能的分步解答再次尝试寻找问题的答案。回溯法通常用最简单的递归方法来实现,在反复重复上述的步骤后可能出现两种情况:
- 找到一个可能存在的正确的答案
- 在尝试了所有可能的分步方法后宣告该问题没有答案
在最坏的情况下,回溯法会导致一次复杂度为指数时间的计算。
阅读全文
0 0
- 【LeetCode with Python】 Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number Leetcode Python
- leetcode 016 Letter Combinations of a Phone Number(Python)
- [leetcode: Python]17. Letter Combinations of a Phone Number
- 【Leetcode】【python】Letter Combinations of a Phone Number
- LeetCode--Letter Combinations of a Phone Number(Python)
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- Java技术常用设计模式(四)--- 工厂模式
- C. Leha and Function(Round #429 (Div. 2)
- suse linux VNC配置
- 数据结构-哈希表
- 常用数学符号的 LaTeX 表示方法
- 【Leetcode】【python】Letter Combinations of a Phone Number
- 【福利】彩虹秒赞8.0源码破解版(去域名授权) 彩虹云任务系统无任何限制
- 康托展开及其逆运算
- RabbitMQ示例生产者消费者(二)——Exchange fanout方式
- Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构 v3 r819
- 使用Lock锁机制实现 多线程的 多生产 多消费 模式。
- Unity-动画(Animator)
- samba
- Atitit 档案学 档案管理 attilax学习总结