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 ...
随机推荐
- Dynamics CRM2011/2013 站点地图sitemap的翻译
实体.属性字段.ribbon等的翻译可以通过解决方案来解决(具体可见我前面的博客:http://blog.csdn.net/vic0228/article/details/37690913),但解决方 ...
- 【Android 系统开发】Android JNI 之 JNIEnv 解析
. jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/android-ndk-r9d/platforms/android ...
- 【shell脚本练习】grep sed awk
下面是简单学习之后做得练习题,如果有不对的或者说解题思路不好的,请交流啊. Grep 练习 文件名grepfile Steve Blenheim:238-923-7366:95 Latham Lane ...
- Python学习笔记 - 字符串和编码
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #第一行注释是为了告诉Linux/OS X系统, #这是一个Python可执行程序,Windows系统会忽 ...
- Android事件总线分发库EventBus3.0的简单讲解与实践
Android事件总线分发库EventBus的简单讲解与实践 导语,EventBus大家应该不陌生,EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Han ...
- Zigbee技术开发一 设置NV_RESTORE
需要试验一下设置NV_RESTORE之后,终端设备的处理逻辑,在IAR里面设置NV_RESTORE的地方如下
- Android中的Message机制
对于Android的Message机制主要涉及到三个主要的类,分别是Handler.Message.Looper:首先对每个类做一个简单介绍:然后再介绍所谓的Android的Message机制是如何实 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- 测试access函数
测试程序: 测试结果: chown root access.out 将用户ID改为root chmod u+s access.out 打开 set-user-ID位
- ERP-非财务人员的财务培训教(一.二)------财务基础知识
二.基本财务管理知识 第一节 财务管理基础知识(一) 财务与会计的关系 会计的基础知识 (一) 财务与会计的关系 财务与会计的内涵 1.会计 会计工作主要是解决三个环节的问题: 会计凭证 会计账簿 会 ...