class Solution:
def letterCombinations(self, digits: str) -> List[str]:
answers = []
answer = []
letter_map = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
def backtrack(digits, index):
if index == len(digits):
temp_str = ""
for temp in answer:
temp_str += temp
answers.append(temp_str)
return
letters = letter_map[digits[index]]
for letter in letters:
answer.append(letter)
backtrack(digits, index+1)
answer.pop()
if not digits: return []
backtrack(digits, 0)
return answers
Leetcode 17. 电话号码的字母组合
最新推荐文章于 2024-11-01 15:19:21 发布