题目:17. Letter Combinations of a Phone Number
链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
按照九宫格的键盘,找到给定的一串数字对应的可能字符串。比如:
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
python:
from functools import reduce
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
dic=["","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"," "]
def twoComb(s1,s2):
return[x+y for x in s1 for y in s2]
return list(reduce(twoComb,[dic[int(i)-1] for i in digits]))