LeetCode(17)Letter Combinations of a Phone Number

题目如下:

Python代码:

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if not digits:
            return []
        dic = {‘2‘:‘abc‘,‘3‘:‘def‘,‘4‘:‘ghi‘,‘5‘:‘jkl‘,‘6‘:‘mno‘,‘7‘:‘pqrs‘,‘8‘:‘tuv‘,‘9‘:‘wxyz‘}
        result = []
        self.helper(digits,dic,0,"",result)
        return result

    def helper(self,digits,dic,index,temp,result):
        if index==len(digits):
            result.append(temp)
        else:
            s = dic[digits[index]]
            for i in s:
                temp += i
                self.helper(digits,dic,index+1,temp,result)
                temp = temp[:-1]
时间: 2024-10-10 16:53:57

LeetCode(17)Letter Combinations of a Phone Number的相关文章

LeetCode (17)Letter Combinations of a Phone Number

题目 Given a digit string, 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. Input:Digit string "23" Output: ["ad", "ae",

【Leetcode】17、Letter Combinations of a Phone Number

题目 Given a string containing digits from 2-9 inclusive, 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 letter

【Leetcode长征系列】Letter Combinations of a Phone Number

原题: Given a digit string, 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. Input:Digit string "23" Output: ["ad", "ae"

leetcode第18题--Letter Combinations of a Phone Number

Problem: Given a digit string, 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. Input:Digit string "23" Output: ["ad", "ae&

LeetCode(17) - Letter Combinations of a Phone Number

经典的backtracking(回溯算法)的题目.当一个题目,存在各种满足条件的组合,并且需要把它们全部列出来时,就可以考虑backtracking了.当然,backtracking在一定程度上属于穷举,所以当数据特别大的时候,不合适.而对于那些题目,可能就需要通过动态规划来完成. 这道题的思路很简单,假设输入的是"23",2对应的是"abc",3对应的是"edf",那么我们在递归时,先确定2对应的其中一个字母(假设是a),然后进入下一层,穷举3

LeetCode(17):电话号码的字母组合

Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf&qu

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, 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. Input:Digit string "23"Ou

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given