leetcode 每日一题 17. 电话号码的字母组合

Posted nil_f

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 每日一题 17. 电话号码的字母组合相关的知识,希望对你有一定的参考价值。

回溯法

思路:

通过回溯的思维,递归调用枚举出所有可能。

class Solution:
    def letterCombinations(self, digits):
        phone = {\'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\']}
                
        def backtrack(combination, next_digits):
            if len(next_digits) == 0:
                output.append(combination)
            else:
                for letter in phone[next_digits[0]]:
                    backtrack(combination + letter, next_digits[1:])
                    
        output = []
        if digits:
            backtrack("", digits)
        return output

 

以上是关于leetcode 每日一题 17. 电话号码的字母组合的主要内容,如果未能解决你的问题,请参考以下文章

《LeetCode之每日一题》:17. 旋转数组

《LeetCode之每日一题》:16.面试题 17.04. 消失的数字

LeetCode每日一题2020.6.17 1014. 最佳观光组合

leetcode每日一题(2020-05-17):210. 课程表 II

[每日一题2020.06.17] leetcode周赛T3 5438 制作m束花所需的最少天数 二分搜索

Leetcode & Java#面试题17.13 / 309