码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】17. Letter Combinations of a Phone Number

时间:2016-12-23 01:06:35      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:nat   number   span   map   字符   返回   class   img   hone   

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"].

题意:输入一个数字字符串,返回每个数字键之间,所对应字母的所有组合

思路:每遍历到一个数字,把这个数字中的所有字符和原列表中的所有字符串相加

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if digits==‘‘:
            return []
        flag={1:[*],
        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],
        0:[ ]
            }
        t = flag[digits[0]]
        res = t
        for c in digits[1:]:
            t = flag[c]
            res = [i+j for i in res for j in t]
        return res
                        

 

【LeetCode】17. Letter Combinations of a Phone Number

标签:nat   number   span   map   字符   返回   class   img   hone   

原文地址:http://www.cnblogs.com/fcyworld/p/6213346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!