思路:用字典的形式保存号码的映射,实际组合是前一个数字串的组合加上后面一个数字的所有可能组合文章来源:https://www.toymoban.com/news/detail-685819.html
answer_dict={'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']}
class Solution:
def letterCombinations(self, digits: str) -> list[str]:
digits=digits.replace('1','')
if not digits:
return []
digits_list=list(digits)
answer_list=answer_dict[digits_list[0]][:]
for digit in digits_list[1:]:
current_answer=[]
for each_answer in answer_list:
for each_char in answer_dict[digit]:
current_answer.append(each_answer+each_char)
answer_list=current_answer
print(answer_list)
return answer_list
文章来源地址https://www.toymoban.com/news/detail-685819.html
到了这里,关于Leetcode17电话号码的组合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!