[LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- 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的更多相关文章
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- 【LeetCode】17. Letter Combinations of a Phone Number
题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...
- LeetCode:17. Letter Combinations of a Phone Number(Medium)
1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- linux----suid\sgid
1.suid和sgid 都是针对二进制程序来说了,bash脚本不在它的作用范围. 2.如果一个二进制文件设置有suid,那么在userA用户执行它时,会以文件所属用户的身份来执行.sgid同理: 3. ...
- 基于IAP和Keil MDK的远程升级设计
写在前面:三个周之前,我突然想写一个远程升级的程序.那个时候我只是大概知道IAP的意思是在应用编程,但怎么编,我还一无所知.我给自己定下一个个阶段目标,从最基础的代码一点点写起,解决一个又一个的问题. ...
- 一步一步学习SignalR进行实时通信_6_案例
原文:一步一步学习SignalR进行实时通信_6_案例 一步一步学习SignalR进行实时通信\_6_案例1 一步一步学习SignalR进行实时通信_6_案例1 前言 类的定义 各块功能 后台 上线 ...
- Java IO读写中文各种乱码问题 【转】
Java IO读写中文各种乱码问题 转自:http://blog.sina.com.cn/s/blog_484ab56f0101muzh.html java.io.*读写中文各种乱码,很费劲.不完全解 ...
- 练习3.20 a 将中缀表达式转换为后缀表达式
//将中缀表达式转换为后缀表达式 int main() { ; ]={,,,,,,,}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); ) { ...
- 栈和队列的Java实现
一. 栈 1.概念 栈是一种特殊的线性表,它只能在栈顶(top)进行插入(push)和删除(pop)操作. 栈的常用操作: 入栈(push):向栈顶插入元素 出栈(pop):从栈顶删除元素 访问栈顶 ...
- hdu 5115 Dire Wolf(区间dp)
Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...
- 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture)
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
- C++游戏编程(一开篇)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7318264 作者:毛星云 邮箱: h ...
- Java面试题之Struts优缺点
优点: 1. 实现MVC模式,结构清晰,使开发者只关注业务逻辑的实现. 2.有丰富的tag可以用 ,Struts的标记库(Taglib),如能灵活动用,则能大大提高开发效率 3. 页面导航 使系统的脉 ...