创建好字典,然后用map函数就可以直接做了
补充一点,字符串也是可迭代的,所以这里创建d的时候可以不用把value像我这样写成list,直接写’abc’也是ok的
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
d = {'2': ['a', 'b', 'c'], '3': ['d', 'e', 'f'], '4': ['g', 'h', 'i'],
'5':['j', 'k', 'l'], '6': ['m', 'n', 'o'], '7': ['p', 'q', 'r', 's'],
'8':['t', 'u', 'v'], '9':['w', 'x', 'y', 'z']}
if not digits or digits[0] not in d: return None
res = d[digits[0]]
for n in digits[1:]:
s = []
if n not in d: return None
for string in res:
tmp = map(lambda x: (string + x), d[n])
s.extend(list(tmp))
res = s
return res