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 ...
随机推荐
- VB.NET版机房收费系统---报表
报表,即报告情况的表格,简单的说:报表就是用表格.图表等格式来动态显示数据,可以用公式表示为:"报表 = 多样的格式 + 动态的数据". 在没有计算机以前,人们利用纸和笔来记录数据 ...
- PKU 3468 A Simple Problem with Integers
题目大意: 有N,M两个数 Q 表示查询, 后面两个数a b,表示查询从a 到b计算它们的和 C 表示增加 后面三个数a,b,c 表示从a开始,一直到b,每个数都增加c 除了查询要进行输出,增加不 ...
- IE11开发人员工具 js脚本debugger调试
微软在浏览器IE11中对F12开发人员工具的UI进行了彻底的换脸,虽然有诸多的不习惯但也是有方便之处的,比如我们用到的最多的断点调试,在F12调试状态下会加载出页面的js脚本文件,而需要对某个文件中的 ...
- Media Player Classic - HC 源代码分析 5:关于对话框 (CAboutDlg)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- Objective-C的面向对象特性(二)
在Objective-C语言中, 类别.类扩展(也称为匿名类别)以及协议是Objective-C 语言级别支持的模式,用来实现对类进行功能扩展. 一.类别--用来增加方法到已存在类 声明一个类别的语法 ...
- XMPP系列(四)---发送和接收文字消息,获取历史消息功能
今天开始做到最主要的功能发送和接收消息.获取本地历史数据. 先上到目前为止的效果图: 首先是要在XMPPFramework.h中引入数据存储模块: //聊天记录模块的导入 # ...
- android自定义view实现公章效果
上次去一个公司面试,面试官问了一个题,怎么用android的自定义view实现一个公章的效果,据说这是华为之前的面试题,我想了下,要是公章的效果,最外层是一个圆,里面是一个五角星,但是这文字怎么画呢, ...
- PS 色调——颜色运算
通过对三个通道定义不同的运算,使图像的色调改变,进而生成不同色彩的图像. clc; clear all; Image=imread('4.jpg'); Image=double(Image); R=I ...
- Zeroc Ice开发环境搭建
搭建Ice环境 1. Linux(推荐,更接近真实生产环境) 2. Windows(方便学习开发) 下载安装包:https://zeroc.com/downloads (百度网盘链接:http ...
- SVN中更改连接用户
Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在 windows > ...