[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 ...
随机推荐
- 部署nginx+rsyslog补丁
nginx 配置: user nginx; worker_processes 1; syslog local5 nginx; error_log /var/log/nginx/nginx_error. ...
- 【解决方法】You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE)
出现场景: 正常调试是没有问题的,但是在Archive的时候,报出了这个错误. 问题详情: (null): URGENT: all bitcode will be dropped because ‘x ...
- Jquery $.extend的重载方法详述
1 $.extend(result,item1,item2,item3,........) -这个重载方法主要是用来合并,将所有的参数都合并到result中,并返回result,但是这样会破坏res ...
- WebApi个人理解概要
WebApi概要 Global文件的作用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class MvcApplication : System.We ...
- 在unity 脚本中获取客户端的IP地址
需要using System.Net.NetworkInformation;原理就是获取网卡的信息. //下面这段代码是我在百度贴吧找来的,经检验是正确的 string userIp = " ...
- 删除Lb重复的数,用La输出(顺序表)
#include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...
- mysql中随机取出几条数据
SELECT t1.id,title,extName,cover,url FROM shop_articles AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT M ...
- 关于playframework2.5
加入了很多新东西: 1.用akka streams 替换了大部分 iteratee-based async io,当然还有一些模块在用iteratees 2.java 的一些API 做了调整升级,以及 ...
- thinkphp phpexcel导出
近期做一个项目涉及到商品信息的批量导出与导入,遂记录了下来,框架是tp框架3.2.3(tp5.0性质是一样的,无非是加载方法与所放目录不一样罢了),运用的是phpexcel,闲话不多说,上代码 1.首 ...
- [LeetCode]题解(python):005-Longest Palindromic Substring
题目来源: https://leetcode.com/problems/longest-palindromic-substring/ 题意分析: 这道题目是输入一段不超过1000的字符串,输出最长的回 ...