题目:
给定一串数字字符,返回手机九建返回的可能的字符串集合。样例如下
解题思路:
直接按照数字顺序将字符串进行迭代。使用哈希表(即python中的字典)提高效率。
代码(Python):
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if digits=='':
return []
List = ['']
Dict = {'0':' ','1':'*','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
def read_char(c,List):
R_list = []
for i in Dict[c]:
for j in List:
R_list.append(j+i)
return R_list
for i in range(len(digits)):
List = read_char(digits[i],List)
return List