[LeetCode]题解(python):017-Letter Combinations of a Phone Number
题目来源:
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的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 【LeetCode】017. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 【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 ...
- leetcode第18题--Letter Combinations of a Phone Number
Problem: Given a digit string, return all possible letter combinations that the number could represe ...
- LeetCode (17)Letter Combinations of a Phone Number
题目 Given a digit string, return all possible letter combinations that the number could represent. A ...
- [Leetcode]017. Letter Combinations of a Phone Number
public List<String> letterCombinations(String digits) { LinkedList<String> ans = new Lin ...
- 017 Letter Combinations of a Phone Number 电话号码的字母组合
给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...
- LeetCode(17)Letter Combinations of a Phone Number
题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
随机推荐
- 第25周五迷茫定位&转行理论建议
今天下午请假办了无房证明和单身证明,准备开始贷款买房的征程,在犹豫纠结中我选择推进这个事情,之前的经验告诉我生活中可以面临改变或不改变境况的选择是要尽可能的选择改变,因为我还年轻.回来后知乎上看了一个 ...
- hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)
质分解 + 简单计数.当时去比赛的时候太年轻了...这道题都没敢想.现在回过头来做了一下,发现挺简单的,当时没做这道题真是挺遗憾的.这道题就是把lcm / gcd 质分解,统计每个质因子的个数,然后 ...
- OpenStack里的浮动ip
缺省情况下实例会被赋予固定ip,这时并不能保证实例会马上可以从外面访问到,一般来说需要配置防火墙来允许公共ip,然后建立一条NAT规则从公共ip到私有ip的映射.OpenStack引入了一个叫浮动ip ...
- PS学习之图像选区
一. 选区的基本操作 快速选择选区与反选选区.取消选区 选择-->全选 或者 CTRL + A 反选CTRL + SHIFT + I ,取消选区 CTRL + D, SHIFT 执行等比例操作 ...
- hdu 2519 新生晚会 排列组合
通过阶段性计算减少一次性的大值计算 #include <stdio.h> int main() { int t, a, b, i; __int64 c; scanf("%d&qu ...
- 【原创】移除RX filters在C118上面
» 作者:LSX » 原创文章版权归作者所有,未经作者同意请保留以下声明. » 本文链接:http://blog.lishixin.net/?p=1318 » 转载请注明来源:LSX·Blog » & ...
- iOS实践04
第四天 微博数据展示:获取服务器数据,json数据的解析,MVC的使用,自定义cell高度的计算,一些分类的设计.已经是第四天了,虽然每天都有课程,但这个东西也基本完成了一大半吧,一些忘掉的知识也捡起 ...
- 关于两次指针(struct型)传参数的问题
这两天被struct传参给郁闷死了.今天终于解决了. 比如有一个struct如下: struct _ns1__Add_USCORESensorDataArray{ struct xsd__base64 ...
- 关于Staruml与powerdesigner启动使用中的问题
问题描述:启动StarUML时,报System Error.Code:1722.RPC服务器不可用的错误! 如下: 这时候: 只需要开启Print Spooler服务即可!在“控制面板中-->管 ...
- [LeetCode]题解(python):003-Longest Substring Without Repeating Characters
题目来源: https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题意分析: 题目是要求出最长的不 ...