- class Solution:
- def f(self,a,b):
- res=[]
- for i in a:
- for l in b:
- res.append(i+l)
- return res
- def letterCombinations(self, digits: str) -> List[str]:
- a={'2':"abc",'3':"def",'4':"ghi",'5':"jkl",'6':"mno",'7':"pqrs",'8':"tuv",'9':"wxyz"}
- l=len(digits)
- if l==0:
- return []
- res=['']
- for i in digits:
- res=self.f(res,a[i])
- return res
注意初始化结果列表:res = ['']:初始时,结果列表中只有一个空字符串,用于逐步构建组合
- def f(self,a,b):
- return [i+l for i in a for l in b]
列表推导式简化