17. 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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
代码:
每日一题哦,今天做到这个题目,仔细一看,就会找到数字对应的字符串,然后把字符串组合起来,返回一个列表。
想想就不应该很复杂,可是,还是做了一个小时,唉,看来必须每天坚持练习练习!
逻辑很简单,就是用一个列表保存每次两个字符串相加的结果,从digits的第一个元素开始,不断和下一次相加,一直加到digits最后一位元素:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
letter_dic = {2:"abc",3:"def",4:"ghi",5:"jkl",6:"mno",7:"pqrs",8:"tuv",9:"wxyz"}
if len(digits)==0:return []
res_list = [list(letter_dic[int(digits[0])])]
digits = digits[1:]
while len(digits) !=0:
res_list.append(self.str_connect(res_list[-1],list(letter_dic[int(digits[0])])))
digits = digits[1:]
#print (res_list)
return res_list[-1]
def str_connect(self,str_ori,str_add):
res = []
for i in range(0,len(str_ori)):
for j in str_add:
res.append(str_ori[i]+j)
return res
17. 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 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所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [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. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
随机推荐
- 您还在招聘网上海量投简历然后等面试机会吗?那你已经OUT了。
工作也可以来找我们.不行看完这篇. 从毕业到现在,换了2次工作.每次都在为招工组烦恼.找工作这个问题,不管是应届生还是职场老手.都面临一个问题就是找工作的平台.纵观目前的找工作的形式,主流的不外乎就两 ...
- Matlab2015矩阵表示03
1. 矩阵表示 >>行元素分隔: 空格'space'或逗号',' >>列分隔: 分号或回车换行符 2. 冒号表达式 1) start:end 2) start: step : ...
- BZOJ 1078: [SCOI2008]斜堆
1078: [SCOI2008]斜堆 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 770 Solved: 422[Submit][Status][ ...
- C#安全性记录
安全性一直是开发中,重中之重的问题.不过平时用的不算特别多,基本上用个MD5,SSL也就到这了.再次记录一下,以免忘记. MD5多次加密 MD5算法是不可逆算法.应用于密码验证,完整性验证这种特征.这 ...
- codevs 1772 歌词
1772 歌词 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 35痛过以后才知情已难寻吾爱至斯只剩飞花梦影回 ...
- LogStash配置、使用(三)
LogStash配置 官方文档:https://www.elastic.co/guide/en/logstash/current/index.html 查看yum安装路径 rpm -ql logsta ...
- ng-show与ng-if区别
<p>ng-show and ng-if : </p> <div ng-show="isShow">ng-show是否显示</div> ...
- 面试题目——《CC150》树与图
面试题4.1:实现一个函数,检查二叉树是否平衡.在这个问题中,平衡树的定义如下:任意一个结点,其两颗子树的高度差不超过1. 思路:两个方法,第一种速度较快 package cc150; public ...
- iframe框架在IE浏览器,360兼容浏览器下将白色背景设为透明色
<IFRAME ID="Frame1" SRC="transparentBody.htm"></IFRAME> iframe在大部分浏览 ...
- 7 款顶级开源 BI(商务智能)软件和报表工具
在这个信息化时代,每分每秒都产生海量数据.在海量数据中,挖掘出有用的数据,并且能以较人性化.直观的方式展示这些数据,变得尤为重要.本文将介绍 7款顶级开源 BI(商务智能)软件和报表工具,用于商业数据 ...