1.
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
def letterCombinations(digits):
ss = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
re = []
for num in digits:
rr = []
if re.__len__() == 0:
for s in ss[int(num)]:
re.append(s)
else:
for s in ss[int(num)]:
rr.append(s)
if rr.__len__() != 0:
res = []
for i in re:
for j in rr:
res.append(i + j)
re = res
return re
用贪婪法,把每个数字表示的字符串,记录下来,在遍历字符串,拼接字符。