phone number
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的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- 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.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [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 ...
随机推荐
- python循环for,range,xrange;while
>>>range(1,5)#代表从1到5(不包含5) [1,2,3,4] >>>range(1,5,2)#代表从1到5,间隔2(不包含5) [1,3] >&g ...
- [RDLC]一步一步教你使用RDLC(一)
一:加数据集,并且命名为Quotation,如下图所示: 二: 添加一张报表,命名为Quotation,如下图所示: 向报表中添加"表"这一项,如下图所示: 这时就弹出一个选择数据 ...
- 4.5、Libgdx运行日志管理
(原文:http://www.libgdx.cn/topic/47/4-5-libgdx%E8%BF%90%E8%A1%8C%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86) ...
- C#之概述
当前流行的开发语言概述 C#是微软公司为Visual Studio开发平台推出的一种简洁.类型安全的面向对象的编程语言,开发人员可以通过她编写在.NET Framework上运行的各种安全可靠的应用程 ...
- Cocos2D v2.0至v3.x简洁转换指南(一)
在该指南开头,我们假设你应经很熟悉Cocos2d 2.x版本. 我们将指出新版本重要的改变,并且给出一些你已经从Cocos2d 2.x版本中熟知的实现. CCNodes,CCScenes和CCLaye ...
- Picasso 图片加载库
Picasso 英文意思国外一个很有名的画家毕加索的名字,国外项目取名还是很有意思的! 从github新下载的picasso项目有依赖其他第三方开源项目okhttp和okio,这两个项目也是相当经典的 ...
- How Tomcat Works 读书笔记 八 载入器 上
Java的类载入器 详细资料见 http://blog.csdn.net/dlf123321/article/details/39957175 http://blog.csdn.net/dlf1233 ...
- ibatis中多表联接查询
目前,我在做项目的时候,用到了spring + struts2 +ibatis 框架.平时用到的都是一张简单的表,来进行数据的增.删.改.查.而现在突然需要用到其它的一张表,或多张表进行联接查询 ...
- android Native堆
Android 应用开发大家都知道可以通过DDMS来查看应用程序进程占用的内存大小:然而Native 内存并不能在虚拟堆上看到:Android系统基于Linux,这样的话其具备Linux的大多数特性: ...
- EBS-子库存转移和物料搬运单区别
FROM:http://bbs.erp100.com/forum.php?mod=viewthread&tid=261550&extra=page%3D7 EBS-子库存转移和物料搬运 ...