problem description:

you should change the given digits string into possible letter string according to the phone keyboards.

i.e.

input '23'

output ['ad','ae','af','bd','be','bf','cd','ce','cf']

the python solution

first you should realize this a iteration process, so you can use many iterate process to handle this problem.reduce fuction is a important iteration function in python.reduce(function , iterator, start),this is it's base form,the first function must have two parameter, the first parameter will be used to record the result,and the other just to fetch the number in the iterator.start can be omitted, then the fisrt result fetch from the iterator the first num,if not the start means the original result.After know the reduce fuction, we know can use it to solve this problem.

in python:

lists = ['a','b']

for in in 'abc':

  lists += i

return lists

then the lists will be ['aa','ab','ac','ba','bb','bc'],base on this feacture, so give the below solution

class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if digits == '': return []
phone = {'':'abc','':'def','':'ghi','':'jkl',
'':'mno','':'pqrs','':'tuv','':'wxyz'}
return reduce(lambda x,y:[a+b for a in x for b in phone[y]],digits, [''])

this solution is so smart.Thanks to huxley  publish such a great  method to deal with this issue.

I f you still can not understand this method, there is another simple way.

class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if len(digits) == 0:
return []
phone = {'':'abc','':'def','':'ghi','':'jkl',
'':'mno','':'pqrs','':'tuv','':'wxyz'} result = ['']
for i in digits:
temp = []
for j in result:
for k in phone[i]:
temp.append(j + k)
result = temp
return result

phone number的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  10. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

随机推荐

  1. ISLR系列:(4.1)模型选择 Subset Selection

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  2. ARM Linux内核Input输入子系统浅解

    --以触摸屏驱动为例 第一章.了解linux input子系统         Linux输入设备总类繁杂,常见的包括有按键.键盘.触摸屏.鼠标.摇杆等等,他们本身就是字符设备,而linux内核将这些 ...

  3. cat .git/config查看远端服务器信息(git的配置信息:远端服务器连接信息)

    本地git库中,查找其连接的远端服务器信息: 每个git库都会有一个配置信息文件.git/config. cat .git/config,可以看到信息如下: [core]         reposi ...

  4. MTK GPIO 一些理解

    基于6575平台: GPIO驱动程序提供了两个接口: (1)内核空间:所提供的GPIO驱动程序,驱动程序可以调用其他函数 (2)用户空间:用户空间的程序可以通过发送IOCTL   /dev/ mtgp ...

  5. Spring--FileSystemXmlApplicationContext

    //从文件系统或者统一定位资源中获得上下文的定义 public class FileSystemXmlApplicationContext extends AbstractXmlApplication ...

  6. 原生的zfs在rhel6上的安装

    原生的zfs在rhel6上的安装 ZFS(Zettabyte File System)作为一个全新的文件系统,全面抛弃传统File System + Volume Manager + Storage( ...

  7. Linux - mail

    使用者邮件信箱: mail 使用 wall, write 毕竟要等到使用者在在线才能够进行,有没有其他方式来联络啊? 不是说每个 Linux 主机上面的用户都具有一个 mailbox 吗? 我们可否寄 ...

  8. Shell Scripts - 循环while,until,for

    while...do...done until...do...done for...do...done

  9. Linux进程通信学习总结

    http://blog.csdn.net/xiaoweibeibei/article/details/6552498 SYSV子系统的相关概念   引用标识符:引用标识符是一个整数,表示每一个SYSV ...

  10. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...