您的位置:首页 > 编程语言 > Python开发

[LeetCode]题解(python):017-Letter Combinations of a Phone Number

2015-09-24 16:05 1021 查看
[b]题目来源:[/b]
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
[b]题意分析:[/b]

这道题是输入一段数字字符digits,在手机上每个数字所对应不同的字符。具体对应如图:

class Solution(object):
def addDigit(self,digit,ans):
tmp = []
for element in digit:
if len(ans) == 0:
tmp.append(element)
for s in ans:
tmp.append(s + element)
return tmp
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
ans = []
d = {'0':' ','1':'*','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
for element in digits:
ans = self.addDigit(d[element],ans)
return ans


View Code

转载请注明出处:http://www.cnblogs.com/chruny/p/4835631.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: