17. 电话号码的字母组合
题目:给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母
链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number
个人思路
- 先用字典储存每个数字对字母的映射,然后对长度为0和1的digits直接返回结果。对长度大于等于2的digits做循环得出答案即可
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if len(digits) == 0:
return []
dictionary = {
}
for i in range(2,7):
dictionary[str(i)] = [chr(j) for j in range(ord('a')+(-2+i)*3,ord('a')+(-2+<