Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
题目大意:根据电话号码键盘上面的数字对应的拼音字母,给一个str型输入,给出所有可能的输出组合。
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if digits=="":
return []
c=[""]
a={"1":"*","2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz","0":" "}
for i in digits:
b=[]
for j in c:
for k in a[i]:
b.append(j+k)
c=b
return c
字典保存对应法则。b[] c[]两个list。c保存每一个i对应的输出结果。然后下一个i遍历上一次输出的c里面的结果。输出到b里面,然后c=b。AC。