题目来源:

https://leetcode.com/problems/letter-combinations-of-a-phone-number/


题意分析:

这道题是输入一段数字字符digits,在手机上每个数字所对应不同的字符。具体对应如图:

返回所有的数字字符对应的字符的可能。比如输入“123”,那么输出["*ad","*ae","*af","*bd","*be","*bf","*cd","*ce","cf"].


题目思路:

看到这道题目让我想起了向量的笛卡尔乘积。这道题目可以用类似DP的方法去解决。dp[n] = dp[n -1]X最后一个字符对应的字符串,其中"X"代表内积,也就是说f("123") = f("1") X f("2") X f("3").首先建立一个字典,d = {'0':' ','1':'*','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'};然后对利用笛卡尔乘积做法得到答案。


代码(python):

 class Solution(object):
def addDigit(self,digit,ans):
tmp = []
for element in digit:
if len(ans) == 0:
tmp.append(element)
for s in ans:
tmp.append(s + element)
return tmp
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
ans = []
d = {'':' ','':'*','':'abc','':'def','':'ghi','':'jkl','':'mno','':'pqrs','':'tuv','':'wxyz'}
for element in digits:
ans = self.addDigit(d[element],ans)
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4835631.html

[LeetCode]题解(python):017-Letter Combinations of a Phone Number的更多相关文章

  1. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  2. 【LeetCode】017. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  3. 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. leetcode第18题--Letter Combinations of a Phone Number

    Problem: Given a digit string, return all possible letter combinations that the number could represe ...

  5. LeetCode (17)Letter Combinations of a Phone Number

    题目 Given a digit string, return all possible letter combinations that the number could represent. A ...

  6. [Leetcode]017. Letter Combinations of a Phone Number

    public List<String> letterCombinations(String digits) { LinkedList<String> ans = new Lin ...

  7. 017 Letter Combinations of a Phone Number 电话号码的字母组合

    给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...

  8. LeetCode(17)Letter Combinations of a Phone Number

    题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...

  9. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  10. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

随机推荐

  1. m文件转换为C/C++文件的编译、绘图、参数、打包问题总结

    在工程计算相关项目中,常常利用Matlab来完成计算.算法.绘图等功能.使用Matlab来完成这些功能非常简单,Matlab提供的m编程语言功能强大,代码量少.为了在自己的C/C++项目中加入这些功能 ...

  2. MATLAB - 为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像?

    Q:为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像? A:数据类型如果是double,imshow的处理范围是0-1数据类型如果是uint8,imshow的处理范围是0 ...

  3. C# 微信公众平台开发(3)-- 接受/发送消息

    1.接收事件 关注/取消关注事件: 用户在关注与取消关注公众号时,微信会把这个事件推送到开发者填写的URL. 通过微信返回的XML,我们通过代码 就可以作相应的处理: 先获取返回的信息: if (Ht ...

  4. C# 微信公众平台开发(2)-- 微信菜单

    上一篇了解微信开发者中心 URL的配置验证: 验证成功后,就可以对获取的接口权限进行操作 自定义菜单接口可实现多种类型按钮,用的比较多的是 1.click:点击推事件 用户点击click类型按钮后,微 ...

  5. 五子棋Web版的开发(一)---搭建IDEA SSH环境

    最近公司都没啥事,我在完成了控制台版的本地五子棋后(github地址:https://github.com/lkq51/wuziqi_console2),想将他升级成为一个web版的五子棋游戏.因为公 ...

  6. 解决mac 10.11 以后 无法使用未签名第三驱动

    解决 最新版 mac 系统 无法使用未签名第三驱动 10.12.多 我的情况是 10.11.4 Beta (15E27e) 使用绿联usb网卡不正常. 下面的命令为检测驱动是否装载的一些命令.sudo ...

  7. C++内联函数与宏定义

    用内联取代宏: 1.内联可调试: 2.可进行类型安全检查或自动类型转换: 3.可访问成员变量. 另外,定义在类声明中的成员函数自动转化为内联函数. 文章(一) 内联函数与宏定义 在C中,常用预处理语句 ...

  8. UVa 12299 RMQ with Shifts(线段树)

    线段树,没了.. ----------------------------------------------------------------------------------------- # ...

  9. [C#编程参考]把图像转换为数组的两种实现

    当一个程序和一个图片放在一起,无非有两种操作: 第一种,就是传输这个图片,在传输图片之前要首先把这个图片变成byte类型的数组.所以这时候我们用到的是图片的存储的数据,也就是图片属性中的大小.我们并不 ...

  10. Object-c KVC的使用和举例

    如果我们的对象需要使用KVC,必须符合object-c的非正式协议NSKeyValueCoding.我们可以简单的来理解KVC,即所有符合KVC机制的对象都看成一个字典(NSDictionary),对 ...