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

时间:2022-09-20 22:34:37
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Number
https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. ===Comments by Dabay===
比较典型的递归调用。
每次消化掉一个字数,把其对应的字母和已有的字符串做笛卡尔乘积。
''' class Solution:
# @return a list of strings, [s1, s2]
def letterCombinations(self, digits):
def letterCombinations2(digits, strings, d):
if len(digits) == 0:
return strings
letters = d[digits[0]]
new_strings = []
for letter in letters:
for i in xrange(len(strings)):
new_strings.append(strings[i] + letter)
return letterCombinations2(digits[1:], new_strings, d) d = {}
d["0"] = d["1"] = ""
d["2"] = "abc"
d["3"] = "def"
d["4"] = "ghi"
d["5"] = "jkl"
d["6"] = "mno"
d["7"] = "pqrs"
d["8"] = "tuv"
d["9"] = "wxyz"
return letterCombinations2(digits, [""], d) def main():
sol = Solution()
print sol.letterCombinations("23") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]17: Letter Combinations of a Phone Number的更多相关文章

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

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

  2. 【LeetCode】17&period; Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  3. 【一天一道LeetCode】&num;17&period; Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

  4. 【LeetCode】17&period; Letter Combinations of a Phone Number

    题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...

  5. LeetCode:17&period; Letter Combinations of a Phone Number(Medium)

    1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...

  6. Leetcode 17&period; Letter Combinations of a Phone Number&lpar;水&rpar;

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  7. 刷题17&period; Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  8. &lbrack;LeetCode&rsqb; 17&period; Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  9. &lbrack;leetcode 17&rsqb;Letter Combinations of a Phone Number

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

随机推荐

  1. cmd部分提权常用命令

    ipconfig 显示本地IP地址 net start telnet 开telnet服务 net use z:127.0.0.1c$ 映射对方的C盘 net user 查看所有用户列表 net use ...

  2. PV与并发之间换算的算法换算公式

    并发连接数 = PV / 统计时间 * 页面衍生连接次数 * http响应时间 * 因数 / web服务器数量 PV = 并发连接数 * 统计时间 * web服务器数量/ 页面衍生连接次数 / htt ...

  3. ajax提交表单

    $.ajax({ type: "POST", url: action, data: $('#checkout-form').serialize(), success: functi ...

  4. 【leetcode】 Unique Path &vert;&vert;&lpar;easy&rpar;

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. linux系统下php安装mbstring扩展的二种方法

    .执行 复制代码代码如下: yum install php-mbstring 2. 修改php.ini (这一步非常重要, 部分lxadmin版本无法自动修改) 复制代码代码如下: echo ‘ext ...

  6. Zookeeper工作原理一

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  7. MongoDB学习笔记-数据格式及数据类型

    JSON JSON是一种简单的数据表示方式,它易于理解.易于解析.易于记忆.但从另一方面来说,因为只有null.布尔.数字.字符串.数组和对象这几种数据类型,所以JSON有一定局限性.例如,JSON没 ...

  8. JavaScript 覆盖document&period;createElement 方法 解决window&period;close在火狐下不兼容问题)

    近期项目遇到了问题,有个asp.net web程序仅仅能在IE7 执行.如今xp都淘汰了,大家都用IE8-IE11,因此这个web app也须要升级 适应全部IE版本号.照成IE版本号不兼容的问题主要 ...

  9. 使用SevenZipSharp压缩&sol;解压7z格式

    7z格式采用的LZMA算法,号称具有现今最高压缩率.笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压.不得不说,SevenZipSharp的API设计得非常 ...

  10. (线段树 区间运算求点)Flowers -- hdu -- 4325

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others)    Mem ...